Jquery 查找特定父级

Jquery Find a especific Parent

使用 Jquery,我想知道如何获取标签之间的文本 (Take this text clicking in button (btnId)) <h4> </h4> 当我点击按钮 (btnId) 时?因为我得到了太多 很难解决!

<div class = "class1" id = "class1">
    <div class = "class2" id = "class2">

        <div class = "class4" id = "class4">

            <h4 class = "h_reference" id = "h_reference"> Take this text clicking in button (btnId)</h4>

        </div>

    </div>


    <div class = "class3" id = "class3">
        <div class = "class5" id = "class5">
            <div class = "class6" id = "class6">

            </div>

            <div class = "class7" id = "class7">
                <div class = "class8" id = "class8">    
                    <div class = "class9" id = "class9">

                    </div>
                    <button id="btnId" />
                </div>
            </div>
        </div>
    </div>
</div>

如果我没有正确理解你的问题,你想通过 jQuery 在点击时获取给定元素的内部文本。方法如下:

$('#btnId').click(function() {
  var txt = $('#h_reference').text();
  //do what you will with txt here
});

这通过 jQuery 的 $.click 函数将点击处理程序分配给按钮。该处理程序读取 ID 为 h_referenceh4 的文本。从那里,您可以对该文本做任何您想做的事情。