如果子跨度有类,则显示父DIV

时间:2015-05-21 15:17:26

标签: javascript jquery html

我试图弄清楚为什么这不起作用。我有一个<rvml:fill class=rvml firebug-1432221487827="1566" rotate = "t" src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAEJJREFUeNpiPHnyOAMUZANxPRA/ZGFAAFkgFgVhkGAcEPMDMRsQ7wTiHyDBhVCVE4DYA8RgAuLXQPwNiF/CzAEIMABgqgriagn8AQAAAABJRU5ErkJggg==" type = "tile" position = "13861519f,3859415f"></rvml:fill>,如果它有一个或多个divspan类的孩子,我想要切换。

请参阅JSFiddle

.reqiredError
<div class="more-content" style="display:none;">
    <div class="col-xs-5 form-group">
        <label for="dua-status">
            Status: 
            <span class="required">*</span> 
            <span class="requiredError">Required Field</span>
        </label>
        <select id="dua-status">
            <option>Open</option>
        </select>
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

您不需要if声明。

相反,使用:has()使用选择器定位它们。这样做的好处是您不必导航到父级,因为上下文将是具有那些跨度的父div

$(document).ready(function() {
        $('div.more-content:has(.requiredError)').fadeIn(0);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="more-content" style="display:none;">
  <div class="col-xs-5 form-group">
    <label for="dua-status">Status: <span class="required">*</span>  <span class="requiredError">Required Field</span>
    </label>
    <select id="dua-status">
      <option>Open</option>
    </select>
  </div>
</div>

答案 1 :(得分:0)

你可以找到目标元素然后使用.closest()找到目标祖先元素

&#13;
&#13;
$(document).ready(function() {
  $('.requiredError').closest('.more-content').fadeIn(0);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="more-content" style="display:none;">
  <div class="col-xs-5 form-group">
    <label for="dua-status">Status: <span class="required">*</span>  <span class="requiredError">Required Field</span>
    </label>
    <select id="dua-status">
      <option>Open</option>
    </select>
  </div>
</div>
&#13;
&#13;
&#13;

closest vs has