在特定 div 中有点击时发出警报
Alert if there's a click in a specific div
我想在点击特定 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");
});
现在我要"marry"这两个
我的代码:
if (!$("h1:contains('Headline')").one(function) && !$(".productBox").click(function)){
alert:("Alert when h1 contains Headline and ProductBox is clicked")};
取自:Jquery "if this and if that" then do this
旁注:没有唯一的选择器(我可以使用的元素或 ID),这就是我想使用标题的原因。
虽然"headline"不在classproductBox的正上方
您的事件应该只在 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 与 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");
}
});
我想在点击特定 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");
});
现在我要"marry"这两个
我的代码:
if (!$("h1:contains('Headline')").one(function) && !$(".productBox").click(function)){
alert:("Alert when h1 contains Headline and ProductBox is clicked")};
取自:Jquery "if this and if that" then do this
旁注:没有唯一的选择器(我可以使用的元素或 ID),这就是我想使用标题的原因。
虽然"headline"不在classproductBox的正上方
您的事件应该只在 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 与 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");
}
});