嘿我试图让函数只在class2没有被附加到class1时才制定:
$("#div").on('mouseenter', 'class1:not(.class2)', function() {
$(".class1").fadeIn('fast', 0);
anotherfunction();
});
.class1 {
position: relative;
width: 181px;
height: 381px;
background: transparent url(/img/phone2.png) center center no-repeat;
opacity: 1;
cursor: crosshair;
background-size: 100%;
z-index: 1000;
}
.class1 .class2 {
background-color: cyan;
}
.class1 .class3 {
-webkit-transition: opacity 300ms;
transition: opacity 300ms;
position: absolute;
top: 88px;
left: 21px;
right: 20px;
bottom: 92px;
background: transparent;
overflow: hidden;
}

Class3在class1中......可能导致问题吗?
<div class="Class1">
<div class="Class3"></div>
</div>
&#13;
答案 0 :(得分:2)
应该是:
$("#div").on('mouseenter', '.class1:not(.class2)', function() {
^
可能你需要改变
$(".class1").fadeIn('fast', 0);
到
$( this ).fadeIn('fast', 0);
答案 1 :(得分:0)
您可以使用jquery函数.has(selector)通过检查返回数组的length属性是否大于零来检查元素是否具有适合选择器的子元素。例如:
if(!$(".class1").has(".class2").length)
triggerFunction();
但是你应该小心,因为如果任何带有classname&#34; class1&#34;的元素,这个语句将总是返回false。有一个孩子班级&#34; class2&#34;。如果你的元素有id&#34; div&#34;也是classname&#34; class1&#34;的元素,然后您只需使用
检查它的子项if(!$(this).has(".class2").length)
你的.on()方法中的。
答案 2 :(得分:0)
您提交的内容:http://jsfiddle.net/d2ozz2rr/
我的更新版本:http://jsfiddle.net/d2ozz2rr/1/
的差异:
$("body")
代替$("#div")
。 #div
查找id="div"
的元素。您使用.on()
的方式需要一个祖先元素。不确定为什么你没有做$(".class1:not(.class2)").on('mouseenter', function () {
'class1'
缺少.
(如另一个答案所述)