小编San*_*dra的帖子

从iframe访问父窗口的元素(jquery)

我有几个dinamically创建的div,里面有相同的元素:

<div id="(dinamic_id_1)">
    <div class="(element_1)">Some text here</div>
    <input class="(element_2)" />
    <iframe class="(element_3)" src="form.php?id=(dinamic_id_1)" />
</div>

<div id="(dinamic_id_2)">
    <div class="(element_1)">Some text here</div>
    <input class="(element_2)" />
    <iframe class="(element_3)" src="form.php?id=(dinamic_id_2)" />
</div>

...
Run Code Online (Sandbox Code Playgroud)

iframe中有一个表单看起来像这样:

<form id="(dinamic_id)" >
    <input class="(class_input)" />
</form>
Run Code Online (Sandbox Code Playgroud)

我的jquery函数:

$(".class_input").change(function() {
    $(this).closest("form").submit();
});
Run Code Online (Sandbox Code Playgroud)

我希望在父窗口上提交的其他操作,在div中包含的与表单共享相同ID的元素上执行.我必须得到父div中的html,如下所示:

var parent_div = $("div#"+$(this).closest("form").attr("id"), parent.document);
alert($(parent_div).html());
Run Code Online (Sandbox Code Playgroud)

但我无法达到div中的元素,例如:

var parent_div = $("div#"+$(this).closest("form").attr("id"), parent.document);
alert($(parent_div+" .element_1").html());
Run Code Online (Sandbox Code Playgroud)

要么

var parent_div = $("div#"+$(this).closest("form").attr("id")+" .element_1", parent.document);
alert($(parent_div).html());
Run Code Online (Sandbox Code Playgroud)

返回null而不是"Some some here here".

iframe jquery jquery-selectors

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

iframe ×1

jquery ×1

jquery-selectors ×1