如何访问子窗口的dom树? [英] How can I access the dom tree of child window?

查看:98
本文介绍了如何访问子窗口的dom树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码打开一个新窗口:

I open a new window using the following code:

purchaseWin = window.open("Purchase.aspx","purchaseWin2", "location=0,status=0,scrollbars=0,width=700,height=400");

我想访问purchaseWin的dom树,例如

I want to access the dom tree of the purchaseWin, e.g.

purchaseWin.document.getElementById("tdProduct").innerHTML = "2";

它不起作用。我只能这样做:

It doesn't work. I can only do this:

purchaseWin.document.write("abc");

我也试试这个也不行:

 $(purchaseWin.document).ready(function(){

     purchaseWin.$("#tdProduct").html("2");

   });

我该怎么办?

推荐答案

使用jQuery,您必须访问文档的内容您孩子的窗口:

With jQuery, you have to access the contents of the document of your child window:

$(purchaseWin.document).ready(function () {
  $(purchaseWin.document).contents().find('#tdProduct').html('2');
});

没有库,使用纯JavaScript,你可以这样做:

Without libraries, with plain JavaScript, you can do it this way:

purchaseWin.onload = function () {
  purchaseWin.document.getElementById('tdProduct').innerHTML = '2';
};

我认为问题是你在子窗口实际加载之前试图检索DOM元素。

I think that the problem was that you were trying to retrieve the DOM element before the child window actually loaded.

这篇关于如何访问子窗口的dom树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆