繁体   English   中英

window.open()在JS函数中无法正常工作

[英]window.open() doesn't work correctly inside a JS function

我试图为我的网站创建一个更新列表,以便当我单击相关更新时,将打开一个新的附加小html窗口,我将看到完整的更新内容。
这是我写的代码:

index.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="script.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>

<body ng-app="myApp">

<p ng-controller="updatesCtrl">
    <span ng-repeat="x in updates">
        <a href="updateWindow.html" onclick="showUpdate(x.title,x.update)">&#9679; {{x.title}}</a>
        <br><br>
    </span>                 
</p>

<script>updates();</script>

</body>
</html> 

script.js:

function updates(){
    angular.module('myApp', []).controller('updatesCtrl', function($scope) {
    $scope.updates = [
        {title:"update1",update:"update1 content"},
        {title:"update2",update:"update2 content"}
        ];
    });
}

function showUpdate(title, update){ 
    var win=window.open('updateWindow.html','newwindow','width=600, height=350'); 
    win.document.getElementById("title").innerHTML=title;
    win.document.getElementById("update").innerHTML=update;
    return false;
}

updateWindow.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>

<h1 id="title"></h1>
<p id="update"></p>

</body>
</html>  

我在这里有几个问题:
1.当我单击更新链接时,更新窗口替换了主页(index.html)窗口,并且也是一个完整页面。
2.尽管我编写了一个脚本,假设未在这些位置输入html内容,但updateWindow的<h1><p>并未进行任何更改。

我不明白为什么我没有得到这段代码期望的结果。 特别是,我不明白第一个问题:如果我只尝试用以下内容替换index.htmlonclick内容: "window.open('updateWindow.html','newwindow','width=600, height=350'); return false;" -我得到了一个新的附加窗口,其尺寸比预期的要小(我不会在此窗口中获取更新内容,但至少可以解决我的第一个问题)。

我的代码有什么问题?

尝试使用window.open('updateWindow.html','_blank','width=600, height=350'); 代替

在您尝试更新html之前,我认为该窗口未加载: 我的娱乐屏幕截图 你可以看到html页面甚至还没有加载尚未和开发者工具说,有一个Uncaught TypeError: Cannot set property 'innerHTML' of null尝试获得新的页面来更新它onload在更新页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM