2010-03-10 22 views
0
<p title="The test paragraph">This is a sample of some <b>HTML you might<br>have</b> in your document</p> 
txt=document.getElementsByTagName("p")[0].childNodes[0].nodeValue; 


alert("<p>The text from the intro paragraph: " + txt + "</p>"); 

它不起作用。childNodes中的html標記(我如何獲得NodesValue)

我怎樣才能獲得childNodes.nodevalue「這是一些HTML你可能也有文檔中的樣本」

回答

1
var p = document.getElementsByTagName('p')[0], 
    txt = p.innerText || p.textContent; 

alert(txt); 
+0

它工作得很好。 – Jason 2010-03-10 15:46:00

+0

非常感謝。這是工作 – Jason 2010-03-10 15:47:00

0

嘗試document.getElementsByTagName("p")[0].innerText

+0

號說: 「不確定」 – Jason 2010-03-10 15:37:02

0

我試過你的例子,它工作正常,但你應該在etElementsByTagName(「p」)中用''代替「」。 我的代碼是:

<p title="The test paragraph">This is a sample of some <b>HTML you might<br>have</b> in your document</p> 
<input type="button" onclick="alert(document.getElementsByTagName('p')[0].childNodes[0].nodeValue);"/> 
相關問題