jQuery 如何使用jquery获取父元素的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6138989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:27:23  来源:igfitidea点击:

How to get the text of parent element using jquery?

jqueryajaxtextparent

提问by Maverick

I have a div which contains a delete hyperlink, onlick of it, I want the text of the div tag. How is it possible in jquery?!

我有一个包含删除超链接的 div,点击它,我想要 div 标签的文本。在 jquery 中怎么可能?!

<div>sometext<a href="#">delete</a></div>

I want to get the text of div tag 'sometext' and if possible the id of that div too. Any ideas?!

我想获取 div 标签“sometext”的文本,如果可能的话,还有那个 div 的 id。有任何想法吗?!

回答by Tomgrohl

The problem with doing:

这样做的问题:

$(this).parent().text();

is that it will get ALL the text within the div (sometext AND delete).

是它将获取 div 中的所有文本(sometext AND delete)。

I'm assuming you only want the text in the div and not the link.

我假设您只想要 div 中的文本而不是链接。

I've knocked up an example on jsFiddle:

我在 jsFiddle 上举了一个例子:

http://jsfiddle.net/HK794/

http://jsfiddle.net/HK794/

Ideally you might want to wrap the text in a span, like:

理想情况下,您可能希望将文本包裹在一个跨度中,例如:

<div id="div1"><span>sometext</span><a href="#">delete</a></div>

Then your JavaScript would be:

那么你的 JavaScript 将是:

$("a").click(function(e){

    $div = $(this).parent("div");

    id = $div.attr("id");

    text = $div.find("span").text();

    alert( text  );

    e.preventDefault();
});

EDIT

编辑

As @DarthJDG states if you don't want to change your markup, any these would also work:

正如@DarthJDG 所说,如果你不想改变你的标记,任何这些也可以工作:

text = $div.get(0).childNodes[0].nodeValue;

//OR    
text = $div[0].childNodes[0].nodeValue;

//OR
text = $div.get(0).firstChild.nodeValue;

//OR
text = $div[0].firstChild.nodeValue;

//OR

//Gets the first text node
$div.contents().filter(function() {
    return this.nodeType == Node.TEXT_NODE;
}).text();

回答by Emyr

        var content;
        $(this).parent().contents().filter(function() {
            return this.nodeType == 3;
        }).each(
                function(i,el){
                    content = content + $(el).text();
                }
        );

This sets content to the value of any direct children which are text.

这将内容设置为文本的任何直接子项的值。

回答by Mark Schultheiss

mySiblingtext="";
myParentId = "";
    $('a').click(function(){
        var mypart = $(this)[0].previousSibling;
        mySiblingtext= $(mypart).text();
        myParentId = $(this).parent().attr('id');
        alert(mySiblingText+myParentId);
    });

jQuery skips over text nodes for all of its traversal methods ( except .contents() ). To access the previous sibling, then text in this case, regardless of nodeType, you can access the DOM node and then use the .previousSibling property. Then you can access the text of that element.

jQuery 跳过其所有遍历方法的文本节点(除了 .contents() )。在这种情况下,要访问前一个兄弟,然后是文本,无论 nodeType 是什么,您都可以访问 DOM 节点,然后使用 .previousSibling 属性。然后您可以访问该元素的文本。

Access to the Id if the parent is then simple as well.

如果父级也很简单,则访问 Id。

Sample fiddle page: http://jsfiddle.net/MarkSchultheiss/FuQ6g/

示例小提琴页面:http: //jsfiddle.net/MarkSchultheiss/FuQ6g/

NOTE: one advantage of this method is if you have markup like:

注意:此方法的一个优点是,如果您有以下标记:

<div id='aparent'>sometext<a href="#">delete</a>stuff after</div> 

the "stuff after" text is not selected.

未选择“后面的内容”文本。

回答by RJD22

$('a').click(function() {
    var id = $(this).parent('div').attr('id');
    var html = $(this).parent('div').html().replace(/<.*>/g, "");
    alert(html);
});

回答by Matt

Although adding in a span tag would probably more efficent

虽然添加跨度标签可能会更有效

http://jsfiddle.net/tN8Mv/

http://jsfiddle.net/tN8Mv/

$("a").click(function(){
var text = $(this).parent().clone()    //clone the element
        .children() //select all the children
        .remove()   //remove all the children
        .end()  //again go back to selected element
        .text();
    alert(text);
});

回答by Riba

jQueryElement.parent().text();

jQueryElement.parent().text();