在jQuery中查找元素

时间:2018-02-28 03:17:03

标签: jquery

当我点击<div class="corner dirty">按钮时,如何从以下HTML中选择<a class="btn btn-primary active"...</a>

<div class="profile-header col-sm-4 ">
    <div class="corner dirty">
        <span>Sale</span>
    </div>
    <div class="profile-cover">
        <h1>01</h1>
        <p>-</p>
    </div>
    <div class="promo-footer-white">
        <div class="row no-gutter">
            <div class="col-sm-12">
                <div class="text-center">

                    <div class="input-group text-center">
                        <div class="switchStatus btn-group" data-id="35" data-module="housekeeping_rooms_status_edit" data-reload="no">
                            <a class="btn btn-primary active" data-status="clean">Propre</a>
                            <a class="btn btn-primary inactive" data-status="dirty">Sale</a>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

我尝试了什么:

$(this).closest('.corner');

但它不起作用。

感谢。

3 个答案:

答案 0 :(得分:0)

$(本).closest(&#39; .profile中报头&#39)。发现(&#39; .corner&#39);

应该为你做的伎俩。

答案 1 :(得分:0)

您可以使用parentsfind

喜欢

$(this).parents('.profile-header').find(".corner")

段:

&#13;
&#13;
$(function() {
	$(".switchStatus a").click(function(){
		console.log( $(this).parents('.profile-header').find(".corner").text() );
	});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="profile-header col-sm-4 ">
  <div class="corner dirty">
    <span>Sale</span>
  </div>
  <div class="profile-cover">
    <h1>01</h1>
    <p>-</p>
  </div>
  <div class="promo-footer-white">
    <div class="row no-gutter">
      <div class="col-sm-12">
        <div class="text-center">

          <div class="input-group text-center">
            <div class="switchStatus btn-group" data-id="35" data-module="housekeeping_rooms_status_edit" data-reload="no">
              <a class="btn btn-primary active" data-status="clean">Propre</a>
              <a class="btn btn-primary inactive" data-status="dirty">Sale</a>
            </div>
          </div>

        </div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这样做......

<script type="text/javascript">
        $(function () {
            $(document).on('click','.btn.btn-primary.active',function () {
               alert($('.corner.dirty').html());
            });
        });
</script>