2014-10-27 61 views
0

我在視圖內和iframe的src中有一個iframe我正在渲染另一個視圖。現在我想獲取頭部h2的數據,這些數據有一些id,並存在於iframe的src內部呈現的第二個視圖中。 請幫我你的想法和建議操作iframe中的src中的視圖內的html元素

回答

0

例子來訪問您的iframe的文檔:

var frame = document.getElementsByTagName("iframe")[0].contentDocument; // first iframe in page 
// or 
var frame = window.frames["myFrame"].document; // if your frame has the name "myFrame" 
// or 
var frame = document.getElementById('myFrame').contentWindow.document; // if your frame has the id "myFrame" 

然後,您可以訪問的iframe裏面的H2標籤:

var h2 = frame.getElementsByTagName("h2")[0]; 

jQuery的解決方案:

var frame = $("selector").contents(); // where selector is "#myFrameID" or ".myFrameClass", etc... 
var h2 = frame.find("h2"); 
+0

雖然我已經做了,但你的jQuery解決方案匹配perfec跟我做的一樣。我在最後添加了一個額外的.html()。 感謝您的回答。我會標記它 – Sidarth 2014-10-28 06:25:18

+0

雖然我已經做到了,但您的jQuery解決方案與我所做的完全匹配。我在最後添加了一個額外的.html()。 – Sidarth 2014-10-28 06:26:23