2009-08-12 103 views

回答

5

.html()是要返回的innerHTML - 这将包括内部的任何一个标签,你也许可以做这样的事情,虽然:

// clone the matched div: 
var copy = winner.clone(); 
// remove all A tags from the DOM 
copy.find("a").remove(); 
// get the html. 
var noanchors = copy.html(); 

而且 - 如果你想获得在A中的文本还是 - 但不是A本身 - 你可以使用:

// for each A tag 
copy.find("a").each(function() { 
    //insert the text within ourselves to the document directly before us. 
    $(this).before($(this).text()); 
    // then delete ourselves 
    $(this).remove(); 
}); 

虽然这实际上可能会有点混乱,如果在<a>有其中的任何其他标签 - 它应该说明的想法。

+0

谢谢,克隆()在这种情况下是完美的。为什么我没有想到这个?根据你的第二个代码块,它对于我需要编写的下一个逻辑有用。 – 2009-08-12 12:27:20

0

这是可能的,但不会给你你期望的结果。 not('a')会从您的winner集合中筛选a标签,但html()会返回集合中第一个项目的值。

因此,如果您winner[div#i1 div#i2 a div#i3 a]not('a')将这套减少[div#i1 div#i2 div#i3]html()将返回div#i1 HTML内容。

如果你想从HTML中删除锚,我认为你应该首先得到的是使用.html()方法,然后做一个替换的正则表达式,将剥离锚定返回的值。

0

或者,你可以使用这样的事情:

var noanchors = ''; 
winner.not('a').each(function() { noanchors = noanchors + $(this).html();) 

获得非一个元素的HTML的串联。但是,这可能会错过各种标签封装项目之间的任何顶级文本。