开发者

jQuery append or appendTo after an img

I cannot get this simple append working. I'm trying to add two break tags AFTER the first Image.

开发者_开发知识库 This is a link

<script type="text/javascript">
$(document).ready(function(){

  $('.photosize').find('img:first').append('<br/><br/>');
});
</script>


append() inserts the elements as a child of the element it applies to. Use after() instead:

$('.photosize').find('img:first').after('<br/><br/>');


This already works, with caveats:

1) When you call $.append() it appends the string to the innerHTML of the element you are appending to. So for this instance it will add two line breaks to the innerHTML of the image element. Try using $.after() instead:

$('.photosize').find('img:first').after('<br/><br/>');

2) The :first selector doesn't work in many (if not all) versions of IE. You can fix this by selecting it using an ID or class instead of the pseudo class :first


Do this (it's tested and works):

$('.photosize img').first().after('<br/><br/>');

Hope this helps. Cheers

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜