0

Hello:) Im a javascript newbie and I'm trying to change a div ID with a script. I have a div with the id "change", and onClick I would like to change the ID "changed", and that I could do, but I would like to click again, and it will return to receive the ID "change". Can you help me?

This is how I did:

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

Thanx.

4

2 回答 2

3

You should not change the id -- instead use the class attribute.

于 2012-07-20T18:11:31.090 回答
0

This would also work:

$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';
  });
});
于 2012-07-20T18:19:13.397 回答