Jquery Find a especific Parent



使用Jquery,我想知道如何在标签之间获取文本(单击按钮(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的文本。从那里,您可以对该文本执行所需的操作。

相关内容

  • 没有找到相关文章

最新更新