简体   繁体   中英

How to set new Id of div using its old id dynamically

I want to set new id to div using its old Id?

$("#oldId").attr("id","newId");

Its not working.

简单的JS

document.getElementById("PreviousName").id="CurrentName";

Your code will work fine, though as of jQuery 1.6 you are encouraged to use prop over attr (see here for discussion).

$(function() {
    $('#test').prop('id', 'test2');
    $('#test2').fadeOut(3000);
});

Fiddle here

Take a look at this question. Can I change the ID of an HTML element using Javascript? Make sure you re-execute your jquery function after the change. Maybe you're working on a 'cached' javascript-object, not the DOM object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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