使用jQuery更改元素的ID [英] Changing an element's ID with jQuery
问题描述
我需要使用jQuery更改元素的ID。
I need to change an element's ID using jQuery.
显然这些不起作用:
jQuery(this).prev("li").attr("id")="newid"
jQuery(this).prev("li")="newid"
我发现我可以通过以下代码实现:
I found out that I can make it happen with the following code:
jQuery(this).prev("li")show(function() {
this.id="newid";
});
但这对我来说似乎不对。必须有更好的方法,不是吗?此外,如果没有,我可以使用什么其他方法而不是显示/隐藏或其他效果?显然我不想每次都显示/隐藏或影响元素,只是为了更改它的ID。
But that doesn't seem right to me. There must be a better way, no? Also, in case there isn't, what other method can I use instead of show/hide or other effects? Obviously I don't want to show/hide or affect the element every time, just to change its ID.
(是的,我是一个jQuery新手。)
(Yep, I'm a jQuery newbie.)
编辑
我不能在这种情况下使用类,我必须使用ID。
Edit
I can't use classes in this case, I must use IDs.
推荐答案
您的语法不正确,您应该将值作为第二个参数传递:
Your syntax is incorrect, you should pass the value as the second parameter:
jQuery(this).prev("li").attr("id","newId");
这篇关于使用jQuery更改元素的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!