-2

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

<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>
4

1 回答 1

1

如果我正确理解了您的问题,您希望通过 jQuery 在点击时获取给定元素的内部文本。就是这样:

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

这通过 jQuery 的函数为按钮分配了一个点击处理程序$.click。该处理程序读取h4带有 id的文本h_reference。从那里,您可以使用该文本做您想做的事情。

于 2016-03-18T15:00:56.177 回答