[英]Why the toggle is not working with other DIVs?
Here i have a fiddle link ! 在这里,我有一个小提琴链接! The problem is only 1st top div is working..
问题是只有第一个top div在起作用。
[http://jsfiddle.net/5Ux8L/4/][1] [http://jsfiddle.net/5Ux8L/4/][1]
HTML- HTML-
<div id="top">top </div>
<div id="box">box </div>
<div id="top">top </div>
<div id="box">box </div>
jQuery- jQuery-
$(document).ready(function(){
$("#top").click(function (){
$("#box").toggle();
});
});
Use common class instead of ids, you should not have duplicate ids . 使用通用类而不是ID, 您不应具有重复的ID 。
$(document).ready(function(){
$(".top").click(function (){
$(this).next().toggle();
});
});
You cannot have elements with the same id. 您不能使用具有相同ID的元素。 Change
top
from an id
to a class
. 将
top
从id
更改为class
。
HTML 的HTML
<div class="top">top </div>
<div class="box">box </div>
<div class="top">top </div>
<div class="box">box </div>
Javascript Java脚本
$(document).ready(function(){
$(".top").click(function (){
$(this).next(".box").toggle();
});
});
JS Fiddle: http://jsfiddle.net/5T3K2/ JS小提琴: http : //jsfiddle.net/5T3K2/
$(document).ready(function(){
$(".top").click(function (){
$(this).next(".box").toggle();
});
});
Don't use id name for multipal divs. 不要对多位div使用id名称。 Always use class.
始终使用类。 By the way here is your working code.
顺便说一下,这是您的工作代码。
<div class="top">top </div>
<div class="box">box </div>
<div class="top">top </div>
<div class="box">box </div>
jQuery jQuery的
$(document).ready(function(){
$(".top").click(function (){
$(this).next().toggle();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.