简体   繁体   English

通过jQuery获取父李文本

[英]get parent li text by jquery

I need alert thisparenttext in following code by clicking on clickedlicontent 我需要在下面的代码警报thisparenttext点击clickedlicontent

<script>
 $("#one").live("click",function(){
 var partxt=$(this).parent().parent().text();
 alert(partxt);
 });
</script>
<ul>
    <li>thisparenttext<ul><li id="one">clickedlicontent</li></ul></li>
    <li>bb</li>
</ul>

In other word, I want take only the text of first parent of clicked li, not all its all html codes. 换句话说,我只想要单击li的第一个父代的文本,而不是全部HTML代码。

What you need to do is clone the element, remove all it's children and use $.text() to get the remaining text: 您需要做的是克隆该元素,删除其所有子元素,然后使用$.text()获得其余文本:

$("#one").live("click", function(){
    var partxt = $(this).parent().parent().clone().children().remove().end().text();
    alert(partxt);
});

This takes the parent of the parent and clones it. 这将采用父级的父级并将其克隆。 This is so that when we do .children().remove() it doesn't affect the displayed elements. 这样一来,当我们执行.children().remove()它不会影响显示的元素。 .end() will "re-select" the parent of the parent of $(this) and .text() gets the text. .end()将“重新选择” $(this)的父级的父级,而.text()将获取文本。

You can try this. 你可以试试看

$("#one").live("click",function(){
     //It will filter parent li's content to get only text node and get its text
     var partxt = $(this).parent().parent().contents().filter(function(){
                       //return true only if text node
                       return this.nodeType == 3;
                  }).text();
     alert(partxt);
});

Working demo - http://jsfiddle.net/TpdfA/ 工作演示-http: //jsfiddle.net/TpdfA/

Side Note: 边注:

If you are using jQuyer ver 1.7+ then it is adviceable to use on else you can use delegate instead of using live . 如果您使用的是1.7+的jQuy​​er版本,则建议on其他版本上使用on可以使用委托而不是live

Using delegate() 使用delegate()

//Instead of document you can specify any parent container of dynamic elements
$(document).delegate("#one", "click",function(){
     //It will filter parent li's content to get only text node and get its text
     var partxt = $(this).parent().parent().contents().filter(function(){
                       //return true only if text node
                       return this.nodeType == 3;
                  }).text();
     alert(partxt);
});

Using on() (jQuery ver 1.7+) 使用on() (jQuery ver 1.7+)

//Instead of document you can specify any parent container of dynamic elements
$(document).on("click", "#one",function(){
     //It will filter parent li's content to get only text node and get its text
     var partxt = $(this).parent().parent().contents().filter(function(){
                       //return true only if text node
                       return this.nodeType == 3;
                  }).text();
     alert(partxt);
});

References: 参考文献:

This turned into an interesting chain 这变成了一条有趣的链

Dem0: http://jsfiddle.net/fut7y/ Dem0: http//jsfiddle.net/fut7y/

$('#one').click(function() {
    var topLevelText = $(this).parent().parent().clone().children().remove().end().text();

    alert(topLevelText);
});​

Get the HTML, turn it into a string, and split on first HTML start tag and then select first value of array to get the first text string. 获取HTML,将其转换为字符串,然后分割第一个HTML start标记,然后选择array的第一个值以获取第一个文本字符串。

$(document).on("click", "#one", function(){
   var partxt = new String($(this).parent().parent().html()).split('<')[0];
   alert(partxt);
});​

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM