Javascript 选择没有特定子体的元素内容

Javascript 选择没有特定子体的元素内容,javascript,jquery,Javascript,Jquery,我正在尝试选择父元素。搜索结果项并从选择中跳过其中的h3标记和btn灰色类。我的密码在这里 $('div.search-result-item:not(h3 .btn-grey)').click(function(e){ console.log('1'); }); 但它选择了h3和btn灰色,我做错了什么 我的HTML在这里 <div data-id="52c53ccfaea0837f318b470c" class="search-result-item ">

我正在尝试选择父元素。搜索结果项并从选择中跳过其中的h3标记和btn灰色类。我的密码在这里

$('div.search-result-item:not(h3 .btn-grey)').click(function(e){
    console.log('1');
});
但它选择了h3和btn灰色,我做错了什么

我的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>

你误解了正在发生的事情。将偶数绑定到父元素时,发生在子元素上的所有事件都将冒泡到父元素并触发父元素上的事件。使用:not不会改变这个事实。相反,您需要在事件处理程序中测试event.target,以确保目标元素不是您不想触发事件的元素之一

$('div.search-result-item').click(function(e){
    if ( !$(e.target).is("h3,.btn-grey") ) {
        console.log('1');
    }    
});

你误解了正在发生的事情。将偶数绑定到父元素时,发生在子元素上的所有事件都将冒泡到父元素并触发父元素上的事件。使用:not不会改变这个事实。相反,您需要在事件处理程序中测试event.target,以确保目标元素不是您不想触发事件的元素之一

$('div.search-result-item').click(function(e){
    if ( !$(e.target).is("h3,.btn-grey") ) {
        console.log('1');
    }    
});

试试这个。通过检查触发事件的元素,在触发事件后执行筛选:

$('div.search-result-item').click(function(e){
    if (!$(e.target).is('h3,.btn-grey')) {
        console.log('1');
    }
});

试试这个。通过检查触发事件的元素,在触发事件后执行筛选:

$('div.search-result-item').click(function(e){
    if (!$(e.target).is('h3,.btn-grey')) {
        console.log('1');
    }
});

所以你不想在点击h3和.btn grey时触发点击事件?是的,我不想在点击h3和.btn grey时触发点击事件。你不想在点击h3和.btn grey时触发点击事件?是的,我不想在点击h3和.btn grey时触发。btn grey不是h3的后代。我在大约8.5分钟前修复了这个问题。btn grey不是h3的后代。我在大约8.5分钟前解决了这个问题