Check if parent element contains certain child element using jQuery

前端 未结 5 784
猫巷女王i
猫巷女王i 2020-12-13 09:21
Blah

I want to set $(\'#

相关标签:
5条回答
  • 2020-12-13 09:39
    <script>
        var test = $('#example #test').length();
        if(test !== 0){
            $('#another').hide();
        }
    </script>
    
    0 讨论(0)
  • 2020-12-13 09:41

    Use length

    if ($('#example').find('#test').length) {
        // found!
    }
    
    0 讨论(0)
  • 2020-12-13 09:44

    it's pretty simple.

    $(document).ready(function(){
        if($('#example').children('#test').length > 0)
        {
            $('#another').hide();
        }
    });​
    

    http://jsfiddle.net/8rrTp/

    0 讨论(0)
  • 2020-12-13 09:45

    jQuery manual suggests to use:

    if ($('#parent').has('#child').length) {
        //do something
    }
    

    http://api.jquery.com/has/

    0 讨论(0)
  • 2020-12-13 09:56

    I think what you are looking for is the following

    if (jQuery.contains($('#example'), $('#test')) {
        $('#another').hide();
    }
    

    This might work as well, not sure off the top of my head.

    if ($('#test', $('#example')).size() > 0) {
        $('#another').hide();
    }
    
    0 讨论(0)
提交回复
热议问题