Jquery找一个特定的父母

时间:2016-03-18 14:57:46

标签: jquery parents

使用Jquery,我想知道如何在标签之间使用文本(在文本中点击按钮(btnId)) 单击按钮(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>

1 个答案:

答案 0 :(得分:1)

如果我正确理解你的问题,你想通过jQuery获取点击给定元素的内部文本。以下是:

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

这通过jQuery的$.click函数为按钮指定了一个单击处理程序。该处理程序读取标识为h4的{​​{1}}的文本。从那里,你可以用那个文字做你想做的事。