添加和使用jQuery删除属性 [英] Add and remove attribute with jquery
问题描述
这是我的按钮:
<button id="add">Add</button>
<button id="remove">Remove</button>
这是我的JavaScript code:
this is my JavaScript code:
<script type="text/javascript">
$(document).ready(function(){
/*** Add attribute ***/
$("#add").click(function(){
$("#page_navigation1").attr("id","page_navigation1");
});
/*** Remove attribute ***/
$("#remove").click(function(){
$("#page_navigation1").removeAttr("id");
});
});
</script>
这是我的html code:
and this is my html code:
<div id="page_navigation1">next</div>
我的问题是,当我点击删除按钮,一切都很好和ID将被删除,
但点击删除按钮后,当我点击添加按钮,code未工作,没有任何反应!
My problem is, when I click on Remove button everything is fine and ID will be removed, but after click on remove button, when I click on Add button, code is not working and nothing happens!
我需要添加和删除ID蒙山这个code,但我可以删除ID,我不能添加ID。我需要一些帮助,还有一件事,
I need to add and remove ID whith this code, but I can just Remove ID, I cant add ID. I need some help for this, and one more thing,
我怎么可以添加CSS样式的按钮时,按钮活跃?
这样,当我点击删除按钮,我想更改按钮的背景为红色。我需要添加CSS类加载,当按钮活跃删除!我不知道怎么办!
How I can add CSS style for button when button is Active? like this, when I click on Remove button, I want change button background to red. I need to add css class for add and remove when buttons are active! and I dont know how!
推荐答案
这是因为你已经删除了 ID
这就是你如何找到的元素。 code的此行试图添加 ID =page_navigation1
与 ID
命名的元素 page_navigation1
,但它不存在(因为你删除的属性):
It's because you've removed the id
which is how you're finding the element. This line of code is trying to add id="page_navigation1"
to an element with the id
named page_navigation1
, but it doesn't exist (because you deleted the attribute):
$("#page_navigation1").attr("id","page_navigation1");
如果你想添加和删除的一类,使你的&LT; DIV&GT;
红用途:
If you want to add and remove a class that makes your <div>
red use:
$( '#page_navigation1' ).addClass( 'red-class' );
和
$( '#page_navigation1' ).removeClass( 'red-class' );
其中,红级
是:
.red-class {
background-color: red;
}
这篇关于添加和使用jQuery删除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!