简体   繁体   English

这个简单的jQuery代码有什么问题?

[英]What is wrong with this simple jQuery code?

This is a simple jQuery code, what I want to do is hide #bling , but it does not 这是一个简单的jQuery代码,我想要做的是隐藏#bling ,但事实并非如此

<script language="javascript">

 $("document").ready(function() {  

      $('#bling').hide();  


  }); 

</script>

<div id="bling" style="background-color:#FFFF66; width:100px; height:100px;"></div> 

Thanks Dave 谢谢戴夫

$("document")更改$("document") $(document)

I tested the code, and it works fine, eventhough you have the string "document" instead of document and an ancient language attribute on the script tag... 我测试了代码,它工作正常,尽管你有字符串"document"而不是document和脚本标记上的古老language属性...

Use type="text/javascript" on the script tag. 在脚本标记上使用type="text/javascript" You can just send a function to the jQuery object as a shortcut for using the ready function: 您可以将函数发送到jQuery对象,作为使用ready函数的快捷方式:

<script type="text/javascript">

$(function(){
   $('#bling').hide();
});

</script>

However, as it's not this code that is the problem, there is something else in your page that is causing it. 但是,由于问题不在于此代码,因此页面中还有其他内容会导致问题。

  • Check that you have successfully included the jQuery script. 检查您是否已成功包含jQuery脚本。

  • Check that you don't have another element with the id "bling". 检查您是否有另一个ID为“bling”的元素。 An id has to be unique in the page. id必须在页面中是唯一的。

  • Check for Javascript error messages. 检查Javascript错误消息。 In IE check the status bar for a notification. 在IE中,检查状态栏以获取通知。 In Firefox open the Javascript console. 在Firefox中打开Javascript控制台。

Works for me as it is . 适合我。 Try using jquery from jquery site, the code is below. 尝试从jquery站点使用jquery,代码如下。

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script language="javascript">

 $("document").ready(function() {  

      $('#bling').hide();  


  }); 

</script>

<div id="bling" style="background-color:#FFFF66; width:100px; height:100px;">aaaaaaaa</div>

See if it works. 看看它是否有效。

Change: 更改:

$("document").ready(function() { $(“document”)。ready(function(){

//..to.. //..至..

$(function() { $(function(){

Make sure you are including the jQuery core javascript file in your HTML. 确保在HTML中包含jQuery核心javascript文件。 On top of that, your code should look more like this: 最重要的是,您的代码看起来应该更像这样:

<script type="text/javascript">
$(document).ready(function() {  
   $('#bling').hide();  
}); 
</script>

Note the type attribute on the script tag as well as document not in quotes. 请注意脚本标记上的type属性以及不在引号中的document

Make sure jQuery is loading: 确保jQuery正在加载:

<script language="javascript">

 $(document).ready(function() {  

      alert('Can you hear me now?');
      //$('#bling').hide();  


  }); 

</script>

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

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