Jquery 检查图像是否不存在,然后隐藏一个div

Jquery 检查图像是否不存在,然后隐藏一个div,jquery,html,css,Jquery,Html,Css,如果div中有图像,则隐藏div。但是如果图像确实存在,则我需要保持div可见 但它不起作用。这是我的密码: HTML: <table id="FeatureBox"> <tbody> <tr> <td> <div id="productInfoGrid"> <div id="ProductIconsTitle"> <p>PR

如果div中有图像,则隐藏div。但是如果图像确实存在,则我需要保持div可见

但它不起作用。这是我的密码:

HTML:

<table id="FeatureBox">
    <tbody>
    <tr>
      <td>
        <div id="productInfoGrid">
          <div id="ProductIconsTitle">
            <p>PRODUCT FEATURES</p>
          </div><img width="563" height="337" src="http://www.preserveshop.co.uk/images/black-jam-jar-lid-58mm.jpg" alt="http://www.preserveshop.co.uk/images/black-jam-jar-lid-58mm.jpg" style="cursor: -moz-zoom-in"><div style="clear:both;"></div>
        </div>
      </td>
    </tr>
  </tbody>
if ($("#div#productInfoGrid:not(img)").length) {
    $("#productInfoGrid").hide();
}
JSFIDDLE

或者更简单

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

或者更简单

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

您可以使用(参考:)

您可以使用(参考:)

if ($("#productInfoGrid").find('img').length == 0) {
    $("#productInfoGrid").hide();
}
​
笔记
$(“div#productInfoGrid:not(img)”)
应该是
$(“div#productInfoGrid:not(img)”)

if ($("#productInfoGrid").find('img').length == 0) {
    $("#productInfoGrid").hide();
}
​
笔记
$(“#div#productInfoGrid:not(img)”)
应该是
$(“div#productInfoGrid:not(img)”)

更新了我的JSFIDLE并略微编码。。。问题仍然存在。元素不能有两个ID:
\div\productInfoGrid
。另外,
:not()
不是此选项的正确选择器。慢慢浏览jQuery API文档:。稍微更新了我的JSFIDLE和代码。。。问题仍然存在。元素不能有两个ID:
\div\productInfoGrid
。另外,
:not()
不是此选项的正确选择器。慢慢来,浏览jQuery API文档:。我的代码怎么了?如果有图像,如果没有图像,这会起作用吗?是的,它会-它检查
div
是否没有(注意
)图像,如果是,则将其隐藏。如果找到图像-没有任何事情发生并且
div
仍然可见我的代码有什么问题?如果有图像,如果没有图像,这会起作用吗?是的,它会-它检查
div
是否没有(注意
)图像,如果是,则将其隐藏。如果找到图像,则不会发生任何情况,
div
保持可见
$("div#productInfoGrid").has('img').hide();