问题
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!
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 - 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>
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?
回答1:
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);
It works in Firefox and chrome too. There must be some other script or code on your page causing toggle problem.
来源:https://stackoverflow.com/questions/19904442/jquery-toggle-sometimes-does-not-work