jQuery切换有时不工作 [英] jquery toggle sometimes does not work

查看:113
本文介绍了jQuery切换有时不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,有一个绝对的噩梦与下降的jquery菜单。我正在创建一个购物篮,并希望用户切换单击下拉列表框以显示篮子中的项目。 Ive得到的内容,可以做到这一点,但它非常间歇!

Ok, having an absolute nightmare with a drop down jquery menu. I'm creating a shopping basket and want the user to toggle click the dropdown basket to show the items in the basket. Ive got the content and can do this but its very intermittent!

我有它在jfiddle工作,但不是我的本地主机。我认为问题可能来到这里。这是我如何运行代码...

Ive got it to work in jfiddle but not my localhost. I think the problem could be coming here. This is how I run the code...

Joomla2.5 - 我的模板 - index.php调用jquery 1.10.2
Jumi模块 - js代码调用:
cart.js - 它调用:
具有以下代码的Basket.phtml:

Joomla2.5 - My template - index.php Calls the jquery 1.10.2 Jumi Module - js code to call: cart.js - which calls: Basket.phtml with the code below:

<div id="accordion">
   <ul>
     <li>
        The content
          <ul>
             The hidden menu
          </ul>
     </li>
   </ul>
</div>

<script type="text/javascript">
(function($){ 
$(document).ready(function() {
$('#accordion ul > li').on('click', function(){

    $(this).children('ul').toggle();


});
});
})(jQuery);
</script>

我添加了event.preventDefault();到脚本的结尾,这使它在Firefox中工作,但在Chrome它是间歇性的,工作时刷新随机。

I have added event.preventDefault(); to the end of the script and this makes it work in firefox but in chrome it is intermittent, works randomly when refreshed. Could it be my browser loading at different speeds or my code that is incorrect?

推荐答案

检查您的代码,似乎工作正常精细。您可以在这里查看演示: http://jsfiddle.net/RSXX5/

Checked your code, and it seems to working fine. You can see a demo here: http://jsfiddle.net/RSXX5/

(function($){ 
    $(document).ready(function() {
        $('#accordion ul > li').on('click', function(){
            $(this).children('ul').toggle();
        });
    });
})(jQuery);

它也适用于Firefox和Chrome。您的网页上必须有一些其他脚本或代码,导致切换问题。

It works in Firefox and chrome too. There must be some other script or code on your page causing toggle problem.

这篇关于jQuery切换有时不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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