在jquery中更改元素的id?

get*_*way 1 javascript jquery

我想在jquery中更改元素的id,即

$('.aiButton').click(function(){
            $('.aiButton').id('saveold');

<a class="aiButton" id="savenew" href="login.php"><span>Save</span></a></li>
Run Code Online (Sandbox Code Playgroud)

你知道我想从savenew中获取id,saveold这就是你如何做到这一点,我想这就是谢谢!!

Mac*_*Mac 7

我这样做:

$('.aiButton').click(function()
{
    // $(this).removeAttr('id');
    $(this).attr('id', 'id-name-goes-here');
});
Run Code Online (Sandbox Code Playgroud)

  • 如果你覆盖它,`removeAttr()`是多余的. (4认同)

Mar*_*kXA 7

要设置ID,您需要$('.aiButton').attr('id', 'saveold');或在这种情况下更有效this.id = 'saveold'.