check if image does NOT exist then hide a div

前端 未结 4 946
猫巷女王i
猫巷女王i 2021-01-24 15:36

If there is an image in the div then hide the div. But if the image does exist then i need to keep the div visable.

But its not working. Here is my code:

相关标签:
4条回答
  • 2021-01-24 16:05
    $("div#productInfoGrid").has('img').hide();
    

    Demo

    Note

    $("#div#productInfoGrid:not(img)") should be $("div#productInfoGrid:not(img)")

    0 讨论(0)
  • 2021-01-24 16:06
    if ( !$("#productInfoGrid").has("img") ) {
        $(this).hide();
    }
    

    or simpler

    $("#productInfoGrid").not(':has("img")').hide();​
    

    DEMO

    0 讨论(0)
  • 2021-01-24 16:22

    You can use .find() (Reference: http://api.jquery.com/find/)

    if ($("#productInfoGrid").find('img').length == 0) {
        $("#productInfoGrid").hide();
    }
    ​
    
    0 讨论(0)
  • 2021-01-24 16:24
    if ($("#productInfoGrid img").length == 0) {
        $("#productInfoGrid").hide();
    }
    
    0 讨论(0)
提交回复
热议问题