jQuery .wrap(), anchors and IE

2019-08-04 12:39发布

I am trying to wrap an anchor-tag around a block of code using jQuery (v1.2.6) by doing the follwing:

var link = $('<a>').attr('href','http://www.foo.bar');
$('#block').wrap(link);

This works perfectly fine in all browsers but (you guessed it) the Internet Explorer (all versions I can use to test it). I do know from other cases that the IE is hella picky when you insert code via JavaScript, but in this case I just don't know what could be wrong with this simple anchor element? Anyone got an idea? Btw: I already tried generating the anchor via $('<a></a>') as suggested on several other posts unfortunately, makes no difference for me.

Thanks a whole lot!

1条回答
手持菜刀,她持情操
2楼-- · 2019-08-04 13:12

Updating my jQuery to 1.3+ did the trick.

Apparently var a = $('<a>'); in jQuery 1.2.6 and earlier will create a chunk of code that IE considers invalid and therefore will refuse to insert it into the DOM tree.

查看更多
登录 后发表回答