我在head标签中有这个:<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.js"></script>
以加载jquery。
然后我在关闭正文标记之前有这个代码:
$(document).ready(function() {
alert("Ready");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
此脚本适用于jsfiddle,但当我尝试在浏览器(Firefox和Chrome)中运行它时,它不起作用。我尝试从本地文件运行它,然后上传到我的网站,以防万一。
那么还有什么可以让它这么简单的脚本不运行?
解决:使用快捷方式'$(function())'在文档就绪事件上运行了另一个脚本。我没有意识到你不能在不同的脚本中使用它两次。谢谢大家的帮助!
答案 0 :(得分:2)
您的问题在于您的其他脚本。 Javascript的另一部分产生错误,应该在您的控制台中可见。
这是一个示例,显示您可以多次绑定到文档就绪事件。我甚至会使用多种语法。
Description Resource Path Location Type
CoreException: Could not get the value for parameter compilerId for plugin execution default-compile: PluginResolutionException: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one of its dependencies could not be resolved: The following artifacts could not be resolved: org.apache.maven:maven-plugin-api:jar:2.0.9, org.apache.maven:maven-artifact:jar:2.0.9, org.apache.maven:maven-core:jar:2.0.9, org.apache.maven:maven-settings:jar:2.0.9, org.apache.maven:maven-plugin-parameter-documenter:jar:2.0.9, org.apache.maven:maven-profile:jar:2.0.9, org.apache.maven:maven-model:jar:2.0.9, org.apache.maven:maven-repository-metadata:jar:2.0.9, org.apache.maven:maven-error-diagnostics:jar:2.0.9, org.apache.maven:maven-project:jar:2.0.9, org.apache.maven:maven-plugin-registry:jar:2.0.9, org.apache.maven:maven-plugin-descriptor:jar:2.0.9, org.apache.maven:maven-artifact-manager:jar:2.0.9, org.apache.maven:maven-monitor:jar:2.0.9, org.apache.maven:maven-toolchain:jar:1.0, org.codehaus.plexus:plexus-container-default:jar:1.5.5, org.codehaus.plexus:plexus-classworlds:jar:2.2.2: Failure to transfer org.apache.maven:maven-plugin-api:jar:2.0.9 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven:maven-plugin-api:jar:2.0.9 from/to central
&#13;
// The first three examples are syntactically identical. They will fire in
// the order in which they appear, also known as FIFO or First In, First Out.
$(document).ready(function() {
alert("Ready one");
});
$(function() {
alert("Ready two");
});
jQuery(document).ready(function($) {
alert("Ready three");
});
// Note: this syntax should fire first, only requires the jQuery object be loaded
(function($) {
alert("Ready four");
})(jQuery);
// and for good measure...this syntax should always fire last
$(window).load(function() {
alert("Ready five");
});
&#13;
如果我想重新创建OP的问题,我只需要在第一个事件处理程序中创建一个例外。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
&#13;
$(document).ready(function() {
alert("Ready one");
// creating an error here should halt execution before the second function begins
throw "some error";
});
$(function() {
alert("Ready two");
});
&#13;
为了证明这一点,这里是完全相同的片段,抛出线被注释掉并正常工作。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
&#13;
$(document).ready(function() {
alert("Ready one");
// creating an error here should halt execution before the second function begins
//throw "some error";
});
$(function() {
alert("Ready two");
});
&#13;
答案 1 :(得分:0)
使用快捷方式'$(function())'在文档就绪事件上运行了另一个脚本。我没有意识到你不能在不同的脚本中使用它两次。谢谢大家的帮助!
答案 2 :(得分:-3)
它应该工作。尝试将type =“text / javascript”添加到脚本标记中。并确保您的自定义脚本在加载jquery.js
之后