简体   繁体   English

新手陷入jQuery警报,无法在浏览器中提示通知

[英]Newbie stuck with jQuery alerts not prompting notifications in browsers

I started learning jQuery on thenewboston.com and got stuck after a dozen tutorials. 我开始在thenewboston.com上学习jQuery,并在打了12个教程后陷入困境。 When I was learning JavaScript, I had no problem with alerts and prompts - they would pop up in the browser. 在学习JavaScript时,警报和提示没有问题-它们会在浏览器中弹出。 I started having problems after I stared learning jQuery. 在开始学习jQuery之后,我开始遇到问题。

I have a simple code that should count all the elements on the page and prompt user the number of elements. 我有一个简单的代码,应该计算页面上的所有元素并提示用户元素的数量。

For index.html file: 对于index.html文件:

<html>
<head>
</head>
<body>

    <p>Some text.</p>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/selectors.js"></script>
</body>
</html>

For selectors.js file: 对于selectors.js文件:

var count = $('*').length;
alert(count);

jquery.js is filled with code downloaded from the official jQuery site (compressed production version). jquery.js充满了从官方jQuery站点下载的代码(压缩的生产版本)。

The code you see here is provided by thenewboston.com instructor, and I checked it dozens of times for mistakes (the videos appear a bit older, don't know if something drastically changed in the meantime). 您在此处看到的代码是由thenewboston.com讲师提供的,我已经对它进行了数十次错误检查(这些视频看起来有些旧,不知道其间是否发生了重大变化)。 Also, file architecture is fine. 同样,文件体系结构也很好。 Both jquery.js and selectors.js are within js folder, and index.html is in root. jquery.js和selectors.js都在js文件夹中,而index.html在根目录中。

What I try so far after googling: 谷歌搜索后我到目前为止尝试的操作:

  • tried Firefox and Chrome 尝试过Firefox和Chrome
  • tinkered with permission settings in both browsers (turned notifications off and on) 在两个浏览器中都修改了权限设置(打开和关闭通知)
  • tried putting the -script- code within the -head- tags 尝试将-script-代码放入-head-标签中
  • tried changing type="text/javascript" to type="application/javascript" within -script- tags 尝试在-script-标记中将type="text/javascript"更改为type="application/javascript"

Sorry, I changed <> symbols with -- because I had some formatting issues within bulleted lists. 抱歉,我用-更改了<>符号,因为项目符号列表中存在一些格式问题。

I tried researching, but I couldn't find a solution to this problem. 我尝试研究,但是找不到解决此问题的方法。 I found something similar (I think on stackoverflow even), and the OP managed to find a solution by turning his browser notifications on. 我发现了类似的东西(甚至在stackoverflow上也是如此),OP通过打开浏览器通知设法找到了解决方案。

This code works just fine . 这段代码可以正常工作

var count = $('*').length;
alert(count);

You can even run it in the dev console, and it works. 您甚至可以在开发控制台中运行它,并且可以运行。

Okay, nvm guys I found a solution... 好的,我找到了一个解决办法……

I was using Firefox when I was downloading jQuery. 我在下载jQuery时使用的是Firefox。 What happened was that Firefox opened a new tab with all the code after clicking download. 发生的事情是Firefox单击下载后,打开了一个包含所有代码的新标签。 I copy-pasted that code to my empty text file and called it jQuery.js. 我将该代码复制粘贴到我的空文本文件中,并将其命名为jQuery.js。

Now using Chrome, I downloaded it again, and it was the real deal. 现在使用Chrome浏览器,我再次下载了它,确实很划算。 I just renamed it, and put it in the js folder, and voila. 我刚刚将其重命名,并将其放在js文件夹中,瞧。 feeling stupid 感到愚蠢

Thx for the answers and effort guys! 谢谢答案和努力的家伙!

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

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