Jquery在普通父

时间:2016-03-27 15:27:04

标签: javascript jquery

考虑代码:

<div class="wrapper">    
    <div class="target">        
        <div class="sub1">
            <span class="value">
                10
            </span>
        </div>            
        <div class="sub2">
            <button type="button">Trigger</button>
        </div>            
    </div>

    <div class="target">                
        <div class="sub1">
            <span class="value">
                20
            </span>
        </div>            
        <div class="sub2">
            <button type="button">Trigger</button>
        </div>            
    </div>    
</div>

如何选择以下值。如果我点击&#39;按钮&#39;,我希望jquery选择父div class="target"并获取span class="value" .text()

诀窍是不选择直接父级,但能够达到多级父级

1 个答案:

答案 0 :(得分:2)

你可以这样做:

$('button').on('click',function(){

  var text=$(this).closest('.target').find('.value').text();

});