开发者

replaceWith is not replacing the attributes of the top id

开发者 https://www.devze.com 2023-01-13 05:58 出处:网络
<div id=\'one\' class=\'foo\'>Hello</div> $(\'#one\').replaceWith(\"<div id=\'one\'>World</div>\")
<div id='one' class='foo'>Hello</div>

$('#one').replaceWith("<div id='one'>World</div>")

Above code is producing

<div id='one' class='foo'>开发者_高级运维World</div>

However the desired output is

<div id='one'>World</div>

Notice that replaceWith is changing the content inside the div but not the attributes of the div itself.


As with the others that have commented on this, I can not reproduce the problem.

$(document).ready(function(){

  $('div').live('click', function(){

    $('#one').replaceWith('<div id="one">World</div>');

  });

});​

That causes

<div id='one' class='foo'>Hello</div> 

this to change to:

<div id='one'>World</div>

However, alternatively you could use this:

$(document).ready(function(){

  $('div').live('click', function(){

    $('#one').removeAttr('class').html('World');

  });

});​

Mind you those are on click, you could remove the click and do it however you like.

See here: http://jsbin.com/adesi3/edit

0

精彩评论

暂无评论...
验证码 换一张
取 消