1

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>

Jクエリ:

if ($("#div#productInfoGrid:not(img)").length) {
    $("#productInfoGrid").hide();
}

JSFIDDLE : http://jsfiddle.net/wJgpr/3/

4

4 に答える 4

5
if ( !$("#productInfoGrid").has("img") ) {
    $(this).hide();
}

またはより簡単

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

デモ

于 2012-07-20T08:47:03.887 に答える
4
if ($("#productInfoGrid img").length == 0) {
    $("#productInfoGrid").hide();
}
于 2012-07-20T08:48:08.573 に答える
2
$("div#productInfoGrid").has('img').hide();

デモ

ノート

$("#div#productInfoGrid:not(img)")する必要があります$("div#productInfoGrid:not(img)")

于 2012-07-20T08:58:12.583 に答える
1

.find() を使用できます(参照: http://api.jquery.com/find/ )

if ($("#productInfoGrid").find('img').length == 0) {
    $("#productInfoGrid").hide();
}
​
于 2012-07-20T08:57:29.380 に答える