javascript - document.title 和 document.write 两者不能同时工作

标签 javascript jquery html

我想通过单击按钮打开一个新窗口。打开成功,但无法同时写入数据和设置标​​题。

有人知道吗?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title And Data Both</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
   $(document).ready(function(){
       $("button").click(function(){
            var filename ="console.log";
           var newwin = window.open("","","width=200,height=100");


            newwin.document.title="Title";            
            newwin.document.write("<h1>XXXX</h1>");
       });
   });
</script>
</head>
<body>
<button type="button">Get Substring</button>
</body>
</html>

最佳答案

document.write() 会覆盖所有现有 HTML(包括标题)。 一种方法是在写入后设置标题。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title And Data Both</title>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("button").click(function () {
                var filename = "console.log";
                var newwin = window.open("", "", "width=200,height=100");

                newwin.document.write("<h1>XXXX</h1>");
                newwin.document.title = "Title";
            });
        });
    </script>
</head>

<body>
    <button type="button">Get Substring</button>
</body>

</html>

关于javascript - document.title 和 document.write 两者不能同时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49692972/

相关文章:

javascript - 如何使用keras/tensorflow将tfjs模型加载到python中

javascript - html5 Canvas绘制图像背景+ 9张图像

html - 如何为照片库设计风格?

php - jquery 搜索和替换

javascript - 在 ReactJS 中使用下划线

javascript - 检测距离鼠标指针最近且带有中断线的线

javascript - 有没有办法将来自 NodeJS Azure Function 的消息延迟放入 Azure 存储队列中?

jquery - Lightbox 或 Fancybox with Rails 4.1.4 仅在刷新后才能工作

javascript - 为什么过渡在 margin-left 属性中不起作用

javascript - Document.write 方法问题