2010-02-26 38 views

回答

1

你可以只使用DOM方法:

var el = document.getElementById("your_element_id"); 
var child, textBits = []; 
for (var i = 0; i < el.childNodes.length; ++i) { 
    child = el.childNodes[i]; 
    if (child.nodeType == 3) { 
     textBits.push(child.nodeValue); 
    } 
} 
window.alert(textBits.join("")); 
+0

謝謝,我試過你的方式,它的工作。我知道它是第一個孩子節點,我對它進行了細化。 '.get(0).firstChild.nodeValue;' – bobmoff 2010-02-26 17:20:55

0

我找到了一種方法!

$('a').get(0).firstElementChild.firstChild.wholeText;

+0

Wops!這可能是Firefox特定的代碼,並沒有在IE中工作。 – bobmoff 2010-02-26 17:08:55

0

根據我的回答this問題,這應該爲你工作。 this.nodeType == 3確定它是一個文本節點。目前這將獲得所有文本節點的文本(但將爲你的例子)。如果在跨度後有文本節點,則必須修改該文件才能獲得第一個文本節點。

var text = $('a') 
    .contents() 
    .filter(function() { 
     return this.nodeType == 3; 
     //return this.nodeType == Node.TEXT_NODE; this works unless using IE 7 
    }) 
    .text(); 
相關問題