如果有'It’点击一个特定的分区
Alert if there's a click in a specific div
如果在特定的分区中有点击,我想提醒。
场景:
<div class="container">
<div class="header"> <h1>Headline<h1> </div>
<div class="productbox"></div>
</div>
我知道如何提醒头条新闻。。。
$("h1:contains('Headline')").one( function() {
alert("ALERT Headline");
});
以及如何提醒Productbox
$(".productBox").click(function){
alert("ALERT Productbox click");
});
现在我想"嫁给"这两个。
我的代码:
if (!$("h1:contains('Headline')").one(function) && !$(".productBox").click(function)){
alert:("Alert when h1 contains Headline and ProductBox is clicked")};
取自:Jquery";如果这个和如果那个";然后做这个
旁注:没有唯一的选择器(我可以使用的元素或ID),这就是我想使用Headline的原因。
尽管"headline"并不直接位于productBox类之上
您的事件应该只在ProductBox上,并且在其中您需要检查prev元素的条件。
$(".productBox").click(function(){
if ( $(this).prev().find("h1:contains('Headline')").size() > 0 )
{
alert("Alert when h1 contains Headline and ProductBox is clicked");
}
});
您应该知道click
是一个事件,只有当有人实际单击时才会调用其中的函数。代码的其余部分将在代码正确启动时执行,请记住这一点。
您的代码包含以下语义部分:
- 分配一个点击处理程序:当有人点击时,这个函数就会被激发
- 检查是否存在具有内容
Headline
的H1 - 如果是,则发出警报
- 点2和3应该只在有人点击时触发(所以当点击处理程序被触发时)
您的代码:
//Registering an on click handler, code inside here fires whe someone clicks
$(".productBox").click(function(){
//check if headline exists in your code (i prefer checking with length, since it returns a jquery array of found elements for this selector)
if ( $("h1:contains('Headline')").length == 1 )
{
//alert user if so
alert("Alert when h1 contains Headline and ProductBox is clicked");
}
});
关于jQuery one()
http://api.jquery.com/one/
其中一个函数与click有点相同:但只有处理程序会触发一次。你可以这样使用它:
//Registering a click handler that only fires once, code inside here fires whe someone clicks
$(".productBox").one("click",function(){
//check if headline exists in your code (i prefer checking with length, since it returns a jquery array of found elements for this selector)
if ( $("h1:contains('Headline')").length == 1 )
{
//alert user if so
alert("Alert when h1 contains Headline and ProductBox is clicked");
}
});
相关文章:
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 如何在单击按钮时显示一个分区,同时隐藏另一个分区
- D3.js:将一个有响应的饼图放在其父分区的中心
- 所有Paragrahps在一个分区
- Jquery.每个分区通过另一个分区内的分区
- 如何获取页面上所有图像的来源,然后将它们全部放在一个分区中
- 将选中的复选框复制到另一个分区中
- 如何在单击图像时在另一个分区中显示相关内容
- 正在获取另一个分区中的span内容
- 如何将ngRepeat内容复制到另一个分区中
- jQuery-在一个分区中查找第一个具有特定ID的分区
- 通过在另一个分区的谷歌地图信息窗口上单击按钮来更改分区的内容
- 将一个分区捕捉到另一个分区
- 导航到外部html页面的一个分区
- 在网页上一次只打开一个分区的脚本
- 如何将一个分区的多个画布内容保存为png图像
- 抓取最近的上一个分区的内容
- 将一个分区的子元素与所有关联的事件一起复制到另一个分区
- 将高度添加到另一个分区
- 如果焦点在下一个分区,则取消focusout事件