使用Javascript重新更改ID

Rechange ID with Javascript

本文关键字:ID 新更改 Javascript 使用      更新时间:2024-01-09

你好:)我是一个javascript新手,正在尝试用脚本更改div ID。我有一个id为"change"的div,在单击时,我想将id更改为"changed",我可以这样做,但我想再次单击,它将返回以接收id"change"。你能帮我吗?

我就是这样做的:

<script> 
$document.ready( function() {
       $("#change").attr('id','changed');
});
});

Thanx。

您不应该更改id,而应该使用class属性。

这也可以:

$document.ready(function () {
  // This assumes the element always starts with the id `change`
  // If this is a poor assumption, change it to $('#change, #changed')
  $("#change").toggle(function () {
    this.id = 'changed';
  }, function () {
    this.id = 'change';
  });
});