I want .click to do nothing IF the user clicks <p>text</p>
inside the div, for everything else I want the div .click to work.
如果用户在div中单击
文本
,那么我想要.click就什么都不做。HTML:
HTML:
<div>
<span>yes</span>
<p>text</p>
</div>
Jquery:
Jquery:
<script>
$(document).ready(function() {
$("div").click(function() {
alert("woohoo!");
});
});
</script>
3 个解决方案
#1
78
$("div").click(function(e) {
if($(e.target).is('p')){
e.preventDefault();
return;
}
alert("woohoo!");
});
check the target of the click. this way you dont need to bind another event.
检查单击的目标。这样您就不需要绑定另一个事件。
#2
2
<script>
$( document ).ready(function() {
$( "div :not(p)" ).click(function( event ) {
alert( "woohoo!" );
});
});
</script>
This implies you don't care about text nodes as children of the <div>
container.
这意味着您不关心文本节点作为
#3
-2
$("div > span").click(function() {
alert("woohoo!");
});
But you'd better provide ids for your html element.
但是您最好为html元素提供id。
#1
78
$("div").click(function(e) {
if($(e.target).is('p')){
e.preventDefault();
return;
}
alert("woohoo!");
});
check the target of the click. this way you dont need to bind another event.
检查单击的目标。这样您就不需要绑定另一个事件。
#2
2
<script>
$( document ).ready(function() {
$( "div :not(p)" ).click(function( event ) {
alert( "woohoo!" );
});
});
</script>
This implies you don't care about text nodes as children of the <div>
container.
这意味着您不关心文本节点作为
#3
-2
$("div > span").click(function() {
alert("woohoo!");
});
But you'd better provide ids for your html element.
但是您最好为html元素提供id。