Jquery“隐藏加载”导致Javascript错误 [英] Jquery "hide on load" causing a Javascript error

查看:122
本文介绍了Jquery“隐藏加载”导致Javascript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果您需要完整的HTML,我会复制它,但它只包含一堆DIV,标题下的项目有一个子类类。



我设法做了什么我需要通过:

  $(document).ready(

$(。subitems)) .hide()

);

但是,在Chrome中,尽管它工作正常,但在控制台中出现以下错误: p>


无法复制格式化)



当我删除上面的代码时,错误会消失。我猜我做错了什么,但我看不到它,到目前为止,它在我测试过的每个浏览器中都能正常工作。



任何建议:


  1. 我做错了什么?

  2. 有没有更好的方法来实现我想要的?


解决方案

你错过了匿名函数( {} )包装,如下所示:

  $(document).ready(function(){ 
$(。subitems)。hide();
});

它试图做 $(。subitems)。hide() .call()在执行准备好的处理程序时被覆盖......但这不是一个函数。相反,它立即执行(不在 document.ready 上),并在 document.ready 尝试运行 .hide()结果

要注意的是,上面也有一个简短形式:

  $(function(){
$( .subitems)。hide();
});


I am trying to write a menu whose subitems vanish when the page is loaded.

If you need full HTML, I will copy it, however it just consists of a bunch of DIVs, the items under the headers have a class of subitems.

I have managed to do what I need via:

   $(document).ready(

    $(".subitems").hide()

   );

However, in Chrome, despite it working fine, I get the following error in the console:

(Using picture as I can't copy the formatting well)

When I remove the above code, the error goes. I guess I have done something wrong, but I can't see it and so far it works in every browser I have tested.

Any suggestions on either:

  1. Have I done something wrong?
  2. Is there a better way to achieve what I want?

解决方案

You're missing the anonymous function(function() { }) wrapper, like this:

$(document).ready(function() {
 $(".subitems").hide();
});

It's trying to do $(".subitems").hide().call() under the covers when it executes the ready handlers...but this isn't a function. Instead, it's executing immediately (not on document.ready) and throwing an error when document.ready tries to run the result of .hide().

Just to note, there's a shorter form of the above as well:

$(function() {
 $(".subitems").hide();
});

这篇关于Jquery“隐藏加载”导致Javascript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆