javascript 使用 JQuery 访问新窗口的 DOM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7788480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:19:11  来源:igfitidea点击:

Using JQuery to Access a New Window's DOM

javascriptjquerypopupwindow

提问by jdw

I am creating a new window that will contain text that user will print. I would like to do something similar to this:

我正在创建一个包含用户将打印的文本的新窗口。我想做类似的事情:

var new_win = window.open();
$(new_win.document).html("Test");

采纳答案by FloydThreepwood

In this case you are trying to access a dom wich has no jQuery enhancement. You need to first load a jquery.js into this document. If done the syntax would be.

在这种情况下,您正在尝试访问没有 jQuery 增强功能的 dom。您需要首先将 jquery.js 加载到此文档中。如果完成,语法将是。

var popup = window.open('/some/url.html');
popup.document.$('body').html('test');

But be very careful, in multi document management and communication are many bugs and inconveniences between the many different browser versions and vendors.

但是要非常小心,在多文档管理和通信中,许多不同浏览器版本和供应商之间存在许多错误和不便。

It would really be better if you keep communication to a absolute minimum, and just load another complete html file into the popup.

如果您将通信保持在绝对最低限度,并且只需将另一个完整的 html 文件加载到弹出窗口中,那真的会更好。

回答by Ali Tabandeh

In our MVC Web application, We need to open a new tab and present a demo from a product. For this purpose, We need To apply some limitation in demo mode and click a button to play a video. we did it like this:

在我们的 MVC Web 应用程序中,我们需要打开一个新选项卡并展示产品的演示。为此,我们需要在演示模式下应用一些限制并单击一个按钮来播放视频。我们是这样做的:

DemoTab = window.open("someUrl/demo", "_blank");
if (DemoTab) {                                
    $(DemoTab.document).ready(function () {        
        $("#hideInDemo", DemoTab.document).removeClass('floatContainer');
        $("#hideInDemo", DemoTab.document).css('display', 'none');
        $("#play", DemoTab.document).trigger('click');
    }); 
}

it only worked on Chrome and Edge.

它只适用于 Chrome 和 Edge。

So we solved the problem by action controller whitch called to load the page. We just passed a parameter by ViewBag to view and on document ready we checked the parameter so then we knew that it called for demo. So we applied our limitations.

所以我们通过调用加载页面的动作控制器解决了这个问题。我们刚刚通过 ViewBag 传递了一个参数来查看,在文档准备好时,我们检查了参数,然后我们知道它调用了演示。所以我们应用了我们的限制。

回答by epquick

To open popup window and modify it's content you can use this code

要打开弹出窗口并修改其内容,您可以使用此代码

var popup = window.open('/' + window.URL_PREFIX + 'print/', '_blank');

popup.onload = function() {
    $(popup.document.body).html('hello');
};