英文:
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 = '<div id="SecondDiv"></div>';
newWindow.document.body.innerHTML = html;
newWindow.document.getElementById("SecondDiv").innerHTML="test content";
If you want to use JQUERY do this:
var html = '<div id="SecondDiv"></div>';
var jquery_object = jQuery(newWindow.document.body);
jquery_object.html(html);
$(jquery_object,"#SecondDiv").html('Hello World');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论