2017-08-30 97 views
1

我一直在使用JavaScript從一個字段獲取信息時遇到了一些問題。 需要的是看看有多少pdiv裏面,因爲我想用這個數字做一些事情。獲取HTML元素(iframe)中元素的數量

的HTML看起來像這樣 - http://i.imgur.com/Fm1ass8.png

,我已經試過(我一直在嘗試在Chrome控制檯)的代碼 - 有時工作,有時沒有。我似乎無法弄清楚爲什麼。

//Getting the iframe element 
var testing = 
document.getElementById("radEditorCenter").getElementsByTagName("*")[2] || 
document.getElementById("radEditor_contentIframe"); 

//Getting the #document element 
var innerDoc = testing.contentDocument || testing.contentWindow.document; 

//Getting the html element 
var HTMLElement = innerDoc.getElementsByTagName("html"); 

//Getting body element 
BodyElement = HTMLElement.getElementsByTagName("body"); 

當我嘗試用最後的代碼片段,它說

VM7957:1 Uncaught TypeError: HTMLElement.getElementByTagName is not a 
function 
at <anonymous>:1:27 
+0

由於問題是由類型引起的,所以投票結束,因爲題外話題。該函數被稱爲'getElementsByTagName' - 元素是複數 - 你在其他地方找到了。 – Quentin

回答

0

如果你想趕上身體所有對節點(與身體的子節點),可以使用

var innerDocPnodes = innerDoc.querySelector('body').querySelectorAll('p').length