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

标签 javascript html angularjs

我尝试为我的网站创建一个更新列表,这样当我单击相关更新时,将打开一个新的附加小 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> 

脚本.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;
}

更新窗口.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.<h1>中没有进行任何更改和 <p> updateWindow 的 - 尽管我编写了一个脚本,该脚本应该在这些地方输入 html 内容。

我不明白为什么我没有通过这段代码得到我所期望的结果。特别是,我不明白第一个问题:如果我只尝试替换 onclick里面的内容index.html与此:"window.open('updateWindow.html','newwindow','width=600, height=350'); return false;" - 我得到一个新的附加窗口,其尺寸符合预期(我不会在该窗口内获得更新内容,但至少解决了我的第一个问题)。

我的代码有什么问题?

最佳答案

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

关于javascript - window.open() 在 JS 函数中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801009/

相关文章:

jquery识别程序语法

html - Angular 路由发生了奇怪的事情

javascript - 实时流程图上的标签?

javascript - Javascript函数参数的简单解释?

jquery - twitter bootstrap 使用工具提示来遵守 html5

jquery - 一键点击; Divs 以相反方向打开

javascript - Angularjs - 页面渲染完成后加载脚本

javascript - 将 Controller 添加到 ngRoute 时出现意外标识符

javascript - React Hook useEffect 在添加缺少的依赖项时进入无限循环

javascript - HTML5 Canvas - 当它超出我的矩形时隐藏文本部分