从iframe中选择具有相同类名的两个DIV中的第二个

Select the second of two DIVs that have the same class name from an iframe

本文关键字:两个 第二个 DIV 选择 iframe 同类      更新时间:2023-09-26

我有一个带有多个div标记的iframe。我无法控制div类名。

为此,我有两个具有相同类名的DIV,我想选择第二个具有该DIV内内容的DIV。直到现在我都无法管理任何内容,我只能使用下面的代码检索整个iframe。

var myIFrame = document.getElementById("iframe");
var content = myIFrame.contentWindow.document.body.innerHTML;
alert(content);   
$("#comment").val(content);

如何检索第二个DIV的内容?

这将为您提供带有.className 的第二个div的内容

$("div.className:eq(1)", content).html();

试试这个:

$('#iframe').contents().find('div.myClass').eq(1)

尝试导航到第二个div,而不是停在正文处。

var content = myIFrame.contentWindow.document.body.childNodes[1].innerHTML;

var content = myIFrame.contentWindow.document.getElementsByTagName("div")[1].innerHTML;

如果iframe是同一个域,则元素很容易作为访问

$("#iframe").contents().find(".somediv").eq(1);

参考:http://simple.procoding.net/2008/03/21/how-to-access-iframe-in-jquery/

相关文章: