我想只有在下面的例子中拉出「88」的數字:jQuery的使用nextSibling拔出數據
<li>
<a href="website">5 stars</a>
(88)
</li>
我曾嘗試:
var theLi= $('li');
var theNumber = $(theLi.get(0).nextSibling).text();
,但沒有出現。它是空白的。
我想只有在下面的例子中拉出「88」的數字:jQuery的使用nextSibling拔出數據
<li>
<a href="website">5 stars</a>
(88)
</li>
我曾嘗試:
var theLi= $('li');
var theNumber = $(theLi.get(0).nextSibling).text();
,但沒有出現。它是空白的。
怎麼樣
var theNumber = $('li').after($('a')).text().replace('(','').replace(')','')
這工作。
這同樣的事情已經有了答案在另一個線程:Using .text() to retrieve only text not nested in child tags
您克隆裏,取出子元素,並提取文本。
Very關閉但括號仍然存在。 – ToddN 2012-02-13 01:03:25
這應該工作,http://jsfiddle.net/elclanrs/bPqDR/:
$('li').clone().children().remove().end().text();
請使用jsFiddle.net在發佈之前測試您的答案,以免誤導人們。你的方法不起作用:http://jsfiddle.net/Gq3nE/ – nopuck4you 2012-02-13 00:54:59
作品:http://jsfiddle.net/hJemv/1/ – 2012-02-13 01:02:44
非常接近但圓括號仍然保留 – ToddN 2012-02-13 01:03:56