Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 2015 javascript对dom元素的访问_Javascript_Visual Studio_Internet Explorer_Windows 10 - Fatal编程技术网

Visual studio 2015 javascript对dom元素的访问

Visual studio 2015 javascript对dom元素的访问,javascript,visual-studio,internet-explorer,windows-10,Javascript,Visual Studio,Internet Explorer,Windows 10,在Visual Studio 2015“Javascript通用Windows”应用程序中,我有以下非常简单的代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <!-- WinJS references --> <link href="WinJS/css/ui-dark.css" rel="stylesheet" /> &l

在Visual Studio 2015“Javascript通用Windows”应用程序中,我有以下非常简单的代码:

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

    <!-- WinJS references -->
    <link href="WinJS/css/ui-dark.css" rel="stylesheet" />
    <script src="WinJS/js/base.js"></script>
    <script src="WinJS/js/ui.js"></script>

    <!-- aaaaa references -->
    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/default.js"></script>
</head>

<body class="win-type-body">

    <div id="myDiv">BEFORE</div>

    <script>            

        window.onload = function () {
            document.getElementById("myDiv").innerHTML = "AFTER";
        };


    </script>
</body>

</html>

之前
window.onload=函数(){
document.getElementById(“myDiv”).innerHTML=“AFTER”;
};
如果我运行应用程序,选择“本地机器”或任何Windows Phone emulator,我会看到“BEFORE”:更改div的innerHtml的行不会执行

否则,如果我在Visual Studio之外的浏览器窗口中执行html文件,我会看到“AFTER”:这适用于所有浏览器,但Internet Explorer 11的行为有一点例外:在这种情况下,我会看到消息“Internet Explorer限制此网页运行脚本或activex控件”,当我单击允许“我看到”之后的内容

为什么这个非常简单的脚本在VisualStudio中不起作用?这是安全限制的问题吗,就像在IE中一样? 为什么我在VisualStudio中根本看不到关于这个问题的任何消息?
如何在Visual Studio中解决此问题?

一个可能的原因可能是js脚本标记内的代码覆盖了window.onload方法。您似乎在脚本标记包含中混合使用了相对路径和绝对路径。因此,在Visual Studio外运行时,这些文件可能不会被包含,因此window.onload不会被覆盖。调试的一些步骤包括:

  • 检查web开发者控制台是否存在错误、网络错误等(来自IE中的内存F12)
  • 删除所有其他脚本标记并进行测试
  • 尝试
    window.addEventListener(“load”,函数{},false);
    而不是
    window.onload

  • 我已经测试了你的代码。这是真的,但你应该在javascript文件中编写代码。只需在default.js的开头移动window.onload。

    这不是一个真正的错误,这就是VS 2015不会发出信号的原因

    事实上,通用应用程序中不允许使用内联脚本,因为它是CSS(跨站点脚本)攻击的一种

    请参阅Microsoft文档链接末尾的第一部分


    讨论了一些解决方法。

    谢谢。我已按如下方式净化了代码,但行为完全相同,F12中没有错误。即使按钮的onclick事件在模拟器和本地计算机中也不起作用。在window.addEventListener(“load”,function(){document.getElementById(“myDiv”).innerHTML=“AFTER”;},false);谢谢。令人难以置信的是,Visual Studio在这种情况下没有显示消息!在我的真实应用程序中,我只需要两行javascript,因此我将它们保留在html文件中。