简体   繁体   English

jQuery .addClass和.removeClass在Chrome或Safari上不起作用

[英]JQuery .addClass and .removeClass not working on Chrome or Safari

I'm trying to show and hide divs using a button. 我正在尝试使用按钮显示和隐藏div。 This code works just fine in Firefox and IE, but for some reason it's not working in Chrome or Safari. 这段代码在Firefox和IE中都可以正常工作,但是由于某些原因,它在Chrome或Safari中不起作用。 The site is using Bootstrap. 该站点正在使用Bootstrap。

 function newAdd(addId, rowId) { var add = "#" + addId.id; var row = "#" + rowId.id; var nextNum = Number(add.charAt(4)); nextNum++; var next = "#add" + nextNum; $(add).addClass("dontShow"); $(next).removeClass("dontShow"); $(row).removeClass("dontShow"); } 
 .dontShow { display: none; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row" id="row1"> <div class="col-sm-12 col-md-12"> <div class="form-group"> <label for="000-000">Item Name:</label> <br /> <input type="text" class="form-control" value=""> </div> </div> </div> <div class="row" id="add1"> <div class="col-sm-1 col-md-1">&nbsp;</div> <div class="col-sm-2 col-md-2"> <a onclick="newAdd(add1,row2)"> <button class="btn" style="text-decoration: none; color: #ffffff;" id="add1" type="button">Add another item?</button> </a> </div> <div class="col-sm-9 col-md-9">&nbsp;</div> </div> <div class="row" id="row2"> <div class="col-sm-12 col-md-12"> <div class="form-group"> <label for="000-000">Item Name:</label> <br /> <input type="text" class="form-control" value=""> </div> </div> </div> <div class="row" id="add2"> <div class="col-sm-1 col-md-1">&nbsp;</div> <div class="col-sm-2 col-md-2"> <a onclick="newAdd(add1,row2)"> <button class="btn" style="text-decoration: none; color: #ffffff;" id="add1" type="button">Add another item?</button> </a> </div> <div class="col-sm-9 col-md-9">&nbsp;</div> </div> <div class="row" id="row3"> <div class="col-sm-12 col-md-12"> <div class="form-group"> <label for="000-000">Item Name:</label> <br /> <input type="text" class="form-control" value=""> </div> </div> </div> 

So what should happen is, the user sees the first row and first button and everything else is hidden. 所以应该发生的是,用户看到第一行和第一个按钮,其他所有内容都被隐藏了。 They fill out the first row, then click on the button. 他们填写第一行,然后单击按钮。 When clicked, the button goes away and the next row and next button appear showing two rows with one add button at the bottom. 单击后,该按钮消失,出现下一行和下一个按钮,显示两行,底部有一个添加按钮。 If they click it again, then the last row should appear and the add button goes away completely. 如果他们再次单击它,则应显示最后一行,并且添加按钮将完全消失。

There are 2 solutions possible to achieve the result. 有两种解决方案可以达到目的。

Solution 1 解决方案1

There are 2 updates that need to be done in your code 您的代码中需要完成2个更新

In HTML 在HTML中

There should be quotes for stirngs like newAdd('add1','row2') 应该有类似newAdd('add1','row2')的搅拌报价

In JS, no need for addId.id 在JS中,不需要addId.id

var add = "#" + addId;

Solution 2 解决方案2

No html update. 没有html更新。 Only in JS, update 仅在JS中更新

var add = "#" + addId.id;

to

var add = "#" + addId[0].id;

Solution 1 - You are passing the id of the element and accessing it in javascript. 解决方案1-您正在传递元素的ID并使用javascript访问它。

Solution 2 - You are passing the HTML Object Collection and then accessing the element and its id property. 解决方案2-您传递HTML对象集合,然后访问该元素及其id属性。

Now which solution to go for? 现在该选择哪种解决方案?

In case you only need Id, you can go for solution 1, as it makes no sense to pass the complete collection just to extract the id which can otherwise be send with the same parameters with just quotes around them. 如果只需要Id,则可以采用解决方案1,因为传递完整的集合仅提取ID毫无意义,否则可以使用相同的参数将其发送,并在其周围加上引号。

However, if you need to get some other properties of the element, then go for solution 2. 但是,如果需要获取元素的其他属性,请使用解决方案2。

The reason why it doesn't work in Chrome or Safari is that add1 , which you are passing to the function, ie 它在Chrome或Safari中不起作用的原因是add1 ,您正在传递给该函数,即

addNew(add1, row1);

does not refer to a single element, but to an element collection: 不是指单个元素,而是指元素集合:

在此处输入图片说明

Why? 为什么? Because you have multiple elements with the same ID. 因为您有多个具有相同ID的元素。 IDs have to be unique! ID必须是唯一的!

Firefox on the other hand simply takes the first element with that ID (which is what I would have expected). 另一方面,Firefox仅采用具有该ID的第一个元素(这就是我所期望的)。


There are a couple of ways to solve this, one of them shown in the other answer, but you should definitely ensure that you are only using unique IDs. 有两种解决方法,一种在其他答案中显示,但您一定要确保仅使用唯一的ID。 I highly recommend to read up on binding event handlers with jQuery: http://learn.jquery.com/events/event-basics/ 我强烈建议您使用jQuery阅读绑定事件处理程序: http : //learn.jquery.com/events/event-basics/

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

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