javascript 打开一个空白窗口,写入它,然后选择写入的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13202487/
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 18:03:51 来源:igfitidea点击:
Open a blank window, write to it, and select the written text
提问by user1795352
I'm trying to open a blank window with Javascript, write text to it, and then select the text that I wrote, all automatically.
我正在尝试使用 Javascript 打开一个空白窗口,向其中写入文本,然后选择我编写的文本,所有这些都是自动的。
I have:
我有:
var myWindow=window.open('');
myWindow.document.write("hey");
myWindow.select();
which opens the window and writes the text, but does not select it.
它打开窗口并写入文本,但不选择它。
回答by ct_
This should do it:
这应该这样做:
var myWindow=window.open('');
myWindow.document.write("<div id='hello'>hey<div>");
var range = myWindow.document.createRange();
range.selectNode(myWindow.document.getElementById('hello'));
myWindow.getSelection().addRange(range);
myWindow.select();
回答by B.Bart
var popup = window.open('message.html',"'" + name + "'",'height=300,width=300,location=no,resizable=yes,scrollbars=yes');
var element = document.createElement('div');
element.setAttribute('id', 'mydiv');
element.appendChild(document.createTextNode('blah blah'));
popup.window.onload = function() {
var win = popup.document.body;
win.appendChild(element);
var el = popup.window.document.getElementById('mydiv').innerHTML;
alert(el); //tested - ouputs 'blah blah'
};