Javascript 如何将 div 值传递给 window.open?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12311268/
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-08-23 07:41:26  来源:igfitidea点击:

how to pass div value to window.open?

javascriptpopup

提问by deidara song

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

我正在尝试将 a 传递DIV给一个新窗口。

JavaScript

JavaScript

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

HTML

HTML

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

Can someone help me out?

有人可以帮我吗?

回答by Bergi

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)

演示在 jsfiddle.net

回答by xdazz

Pass the id of the div to the function.

将 div 的 id 传递给函数。

 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>

回答by jitendra

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();