简体   繁体   English

为什么切换开关不能与其他DIV一起使用?

[英]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 . topid更改为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/

Id of an element must be unique, so use class instead 元素的ID必须唯一,因此请使用类

<div class="top">top</div>
<div class="box">box</div>
<div class="top">top</div>
<div class="box">box</div>

then 然后

jQuery(function ($) {
    $(".top").click(function () {
        $(this).next().toggle();
    });
});

Demo: Fiddle 演示: 小提琴

$(document).ready(function(){
$(".top").click(function (){
    $(this).next(".box").toggle();
});
});

http://jsfiddle.net/5Ux8L/5/ http://jsfiddle.net/5Ux8L/5/

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();
    });
});

Demo 演示版

Use class instead of Id . 使用class代替Id。 Id is unique property. ID是唯一属性。 fiddle 小提琴

 <div class="top">top </div>
    <div class="box">box </div>
 <div class="top">top </div>
    <div class="box">box </div>

$(document).ready(function(){
  $(".top").click(function (){
        $(".box").toggle();
    });
});

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

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