javascript jQuery document.ready 未触发(已包含 jq)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12421469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:09:40  来源:igfitidea点击:

jQuery document.ready not firing (have included jq)

javascriptjqueryhtml

提问by zomboble

So, after traversing google and stackoverflow for a short while, most resources I found were people incorrectly including jQuery, which I included it correct.

因此,在短暂地浏览了 google 和 stackoverflow 之后,我发现的大多数资源都是错误地包含 jQuery 的人,我正确地包含了它。

I have this in my header:

我的标题中有这个:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <?php wp_head(); ?>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title>
  <?php bloginfo('name'); ?>
</title>
<link href="<?php echo home_url(); ?>/css/styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {   
  alert("Hey");
  });
}
  alert("hello");

And everything else I need in there. When I take out the document ready, it alerts hello, however with the doc ready it doesnt alert either. I have never had this problem before with jQuery.

还有我需要的一切。当我拿出准备好的文档时,它会提醒你好,但是当我准备好文档时,它也不会提醒。我以前在使用 jQuery 时从未遇到过这个问题。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Kevin Bowersox

I see an extra curly brace in your code. Try:

我在您的代码中看到一个额外的花括号。尝试:

$(document).ready(function() {   
  alert("Hey");
  });
//} Remove this brace

回答by Shannon

You have a syntax error :) Try this!

你有一个语法错误 :) 试试这个!

$(document).ready(function() {   
  alert("Hey");
});

回答by Joey Cadle

You have an extra bracket right below your alert("Hey") that is causing an error. Remove that bracket and you should be good to go.

您的警报(“嘿”)正下方有一个额外的括号导致错误。取下那个支架,你应该很高兴。

回答by dsgriffin

Change the second <script type="text/javascript">to <script>

将第二个更改<script type="text/javascript"><script>

Replace $(document).ready(function()with $(function ()

替换$(document).ready(function()$(function ()

Take out the extra }

取出多余的 }

回答by Akhil das

$(document).ready(function() {   
    //code  goes here
    alert("Hey");
});

It will work so you should place your code inside ready function.

它将起作用,因此您应该将代码放在就绪函数中。