如何使用jQuery访问新浏览器窗口中的元素?

huangapple go评论60阅读模式
英文:

How to use jQuery to access an element in a new browser window?

问题

可以使用jQuery从原始页面引用新浏览器窗口中的元素吗?

divOnMainPage = document.createElement('div')

divOnMainPage.id = 'FirstDiv'

document.body.appendChild(divOnMainPage)

// 可以使用javascript引用这个div,如divOnMainPage; 或者document.getElementById('FirstDiv'); 或者使用jQuery如$(‘#FirstDiv’)[0]

// 但如果启动了一个新窗口:
newWindow = window.open('','','width=200, height=200')

divInWindow = newWindow.document.createElement('div')

divInWindow.id = 'SecondDiv'

// 可以在MainPage控制台中使用javascript引用这个元素,如divInWindow; 或者newWindow.document.getElementById('SecondDiv'); 但如何使用jQuery获取它呢?$(‘#SecondDiv’)[0] 不起作用。
英文:

Can one refer to an element within a new browser window from the original page using jquery?

divOnMainPage = document.createElement(‘div’)

divOnMainPage.id = ”FirstDiv”

document.body.appendChild(divOnMainPage)

// One can refer to this div with javascript as divOnMainPage; or document.getElementById(‘FirstDiv’);   or with jQuery as $(‘#FirstDiv’)[0]  


// But if a new window is launched:
newWindow = window.open('','','width=200, height=200')

divInWindow = newWindow.document.createElement(‘div’)

divInWindow.id = “SecondDiv”

// Can refer to this in javascript from the MainPage console as divInWindow; or newWindow.document.getElementById(‘SecondDiv’);    but how to get it with jQuery?   $(‘#SecondDiv’)[0] does not work.  

答案1

得分: 1

像这样,它正在工作。

var html = '<div id="SecondDiv"></div>';
newWindow.document.body.innerHTML = html;
newWindow.document.getElementById("SecondDiv").innerHTML = "测试内容";

如果要使用JQUERY,请执行以下操作:

var html = '<div id="SecondDiv"></div>';
var jquery_object = jQuery(newWindow.document.body);
jquery_object.html(html);
$(jquery_object, "#SecondDiv").html('你好,世界');
英文:

Like this, it is working.

var html = &#39;&lt;div id=&quot;SecondDiv&quot;&gt;&lt;/div&gt;&#39;;
newWindow.document.body.innerHTML = html;
newWindow.document.getElementById(&quot;SecondDiv&quot;).innerHTML=&quot;test content&quot;;

If you want to use JQUERY do this:

    var html = &#39;&lt;div id=&quot;SecondDiv&quot;&gt;&lt;/div&gt;&#39;;
    var jquery_object = jQuery(newWindow.document.body);
	jquery_object.html(html);
	$(jquery_object,&quot;#SecondDiv&quot;).html(&#39;Hello World&#39;);

huangapple
  • 本文由 发表于 2023年6月2日 00:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定