本文介绍了基本的javascript警报在Intel XDK代码编辑器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我刚刚开始使用一个新的空白英特尔项目,我还没有开始编码。我只是设置我的文件树并确保通过正确的锚点和脚本路径连接html和javascript页面。我的第一个.js文件不起作用。

I'm starting fresh with a new blank Intel project and I haven't even started coding yet. I'm just setting up my file tree and making sure the html and javascript pages are connected via the right anchors and script paths. My first .js file won't work though.

我已经包含了测试代码和错误的屏幕截图。我知道语法是正确的,因为当我将它放在index.html文件中的< script> 标签时它会起作用。
虽然我收到文件未定义和警告未定义错误或js页面。我不知道这意味着什么。

I've included a screen shot of test code and the errors. I know the syntax is correct because it works when I put it in <script> tags in the index.html file.I'm getting "document not defined" and "alert not defined" errors or the js page though. I don't know what that means.

我认为我的索引文件中的脚本标记src路径不正确,但所有路径都是相对的,注释掉的模板脚本标记为intel提供的索引页面开箱即用,为什么我必须使用绝对路径?

I've considered that my script tag src path in the index file is incorrect, but all the paths are relative in the commented out template script tags intel provides on the index page right out of the box, so why would I have to use an absolute path?

我的路径是:js / Test.js,它是正文之前的最后一个脚本标记。

My path is: js/Test.js and it's the last script tag before the body.

Index.html文件

Index.html file

*****更新****

*****UPDATE****

所以我已经尝试了一些事情,它仍然没有工作,但我设法将我的错误降低到只有一个莫名其妙的缺少分号,如果我放置它将变成不必要的分号错误。
根据第一个屏幕截图,您将看到我没有将文档对象放在显式声明的变量中。一旦我这样做并通过点语法而不是等号访问它然后我停止得到错误。在我进行更改之前,我将此屏幕截图显示在我的工作中。

So I've tried a few things and it's still not working but I HAVE managed to get my errors down to just one inexplicable "missing semicolon", which will turn into an "unnecessary semicolon" error if I place it.Any way as per the first screen shot you'll see that I wasn't placing the document object inside of an explicitly declared variable. Once I did that and accessed it through dot syntax instead of an equal sign then I stopped getting the error. I included this screenshot to show my work before I made the changes.

所以我继续遇到的问题是除非每个函数或dom对象都声明为 Var,我得到一个错误。这包括alert()函数,我认为我从未见过需要以这种方式声明,但我给了代码编辑器它想要的东西,最后的截图是结果。它不起作用,但我没有得到我以前的错误,除了缺少/不必要的分号悖论。删除它或包含它会引发错误。

so the problem I went on to have is that unless every function or dom object was declared with "Var", I'd get an error. This includes the alert() function which I don't think I've ever seen needing to be declared that way, but I gave the code editor what it wanted and this last screenshot is the results. It's not working, BUT I'm not getting the errors I was before, except for the missing/unnecessary semicolon paradox. Removing it or including it throws an error.

推荐答案

可以在<$ c $之前或之后加载JavaScript c> HTML ,但它的完成方式略有不同,具体取决于您的操作方式。

JavaScript can be loaded before or after the HTML, however the way it is done is slightly different depending on how you do it.

例如,如果您希望将JavaScript文件包含在 HTML的 head 文件然后你必须用 DOMContentLoaded 或jQuery的<$ c $包装你的 JavaScript 代码C> $(文件)。就绪()。

For example if you wish to include your JavaScript files within the head of the HTML file then you must wrap your JavaScript code with either DOMContentLoaded or jQuery's $(document).ready().

使用 window.onload 的常见误解不会解决元素未正确加载的问题。

The common misconception of using window.onload will not fix the issue where the elements have not loaded in correctly.

Mozilla开发者网络在页面:

The Mozilla Developer Network states on this page:

这个引用本身应该证明如果你希望在开始操作它之前正确加载完整的DOM,那么 c> c> onload 就不应该依赖于。相反,您应该执行以下操作:

That quote in itself should prove that onload should not be relied on if you want the full DOM to be loaded properly before you start manipulating it. Instead you should do the following:

香草

document.addEventListener("DOMContentLoaded", function (e) {
    /** DOM has been fully loaded here, so manipulation can begin. **/
    /** Your code here. **/
});

jQuery

$(document).ready(function () {
    /** DOM has been fully loaded here, so manipulation can begin. **/
    /** Your code here. **/
});

点击链接,看看vanilla和jQuery之间的区别。

Click this link to see the difference between vanilla and jQuery.

第二个你可以加载JavaScript的方法是在正文中包含所有脚本标记,但是在所有HTML之后,这样就可以保证在HTML之后加载。

The second way you can load JavaScript is by having all the script tags within the body but after all the HTML, that way it is guaranteed to load after the HTML.

示例

尝试这个工作示例我快速编码。

Try this working example I quickly coded up.

HTML

替换正文中的内容 HTML 的标记到以下内容:

Replace the content within the body tag of your HTML to the following:

<button id="myJsTest">Click Me!</button>
<div id="clickCounter">Click Count: 0</div>

用以下内容替换JavaScript的内容:

Replace the contents of your JavaScript with the following:

JavaScript

document.addEventListener("DOMContentLoaded", function() {
    var clickCount = 0;

    document.getElementById("myJsTest").addEventListener("click", function() {
        clickCount++;

        document.getElementById("clickCounter").innerText = "Click Count: " + clickCount;
    });
});

然后使用英特尔XDK中的模拟标签进行测试。

Then use the Emulate tab in Intel XDK to test it.

其他信息

当我使用英特尔XDK并且出现错误时,我很快将文件加载到浏览器并检查控制台。它可以是一个非常有用和有效的方法来压缩那些讨厌的小错误。

When I use Intel XDK and I have an error, I quickly load the file in to the browser and check the console. It can be a really helpful and effective way of squashing those pesky little bugs.

尝试使用 window.alert as alert 窗口对象中定义。

Try using window.alert as alert is defined in the window object.

这篇关于基本的javascript警报在Intel XDK代码编辑器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:34