how to pass div value to window.open?

不打扰是莪最后的温柔 提交于 2019-12-10 11:16:46

问题


I'm trying to pass a DIV to a new window.

JavaScript

function openWin() {
    myWindow=window.open('','','width=200,height=100');
}

HTML

<div id="pass">pass this to the new window</div>
<a href="#" onclick="openWin();">click</a>

Can someone help me out?


回答1:


I think you want this:

var divText = document.getElementById("pass").outerHTML;
var myWindow = window.open('','','width=200,height=100');
var doc = myWindow.document;
doc.open();
doc.write(divText);
doc.close();

(Demo at jsfiddle.net)




回答2:


Pass the id of the div to the function.

 function openWin(id) {
     var divText = document.getElementById(id).innerHTML;
     myWindow=window.open('','','width=200,height=100');
 }



 <div id="pass">pass this to the new window</div>
 <a href="#" onclick="openWin('pass')" />click</a>



回答3:


it worked for me

var divText = document.getElementById("id").outerHTML;
    var myWindow = window.open('','','width=200,height=100');
    var doc = myWindow.document;
    doc.open();
    doc.write(divText);
    doc.close();


来源:https://stackoverflow.com/questions/12311268/how-to-pass-div-value-to-window-open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!