我试图选择父元素 .search-result-item
并跳过其中的 h3 标签和 btn-grey 类。我的代码在这里
$('div.search-result-item:not(h3 .btn-grey)').click(function(e){
console.log('1');
});
但它选择了 h3 和 btn-grey 我做错了什么?
我的 HTML 在这里
<div data-id="52c53ccfaea0837f318b470c" class="search-result-item ">
<div>
<div class="search_result">
<div class="picture"><img width="195" height="130" src="/images/not_available.png" alt=""></div>
<div class="info">
<h3>
<a href="/course-detail/masters/management-with-human-resource-management/full-time/university-of-chester-faculty-of-business-enterprise-and-lifelong-learning/uk/52c53ccfaea0837f318b470c" title="Management with Human Resource Management">
Management with Human Resource Management </a>
</h3>
<p>
<strong>Entry Requirements: </strong>IELTS : 6.5
</p>
<a data-id="52c53ccfaea0837f318b470c" class="request-info" title="Request Info">
<button class="btn btn-yellow">Request Info</button>
</a>
<a class="btn btn-grey" href="/course-detail/masters/management-with-human-resource-management/full-time/university-of-chester-faculty-of-business-enterprise-and-lifelong-learning/uk/52c53ccfaea0837f318b470c" title="Course Details">
Course Details
</a>
</div>
</div><!-- .search_result -->
</div>
</div>
最佳答案
试试这个。通过检查触发事件的元素来在事件触发后进行过滤:
$('div.search-result-item').click(function(e){
if (!$(e.target).is('h3,.btn-grey')) {
console.log('1');
}
});
关于javascript - 选择没有某些后代的元素内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22965996/