开发者

Mixing inline attributes with an attribute object

I should get 3 images here but I get 2. The last one fails:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.1.js" type="text/javascript"></script>
<script type="text/javascript">
$().ready(function() {
    var img = $('<img src="icon.next.gif" url="/" />');
    $("body").append(img);
    img = $('<img />', { src: "icon.next.gif", url: "/" });
    $("body").append(img);
    var img = $('<i开发者_运维技巧mg src="icon.next.gif" />', { url: "/" });
    $("body").append(img);
});
</script>
</head>

<body>
</body>
</html>

I could swear I've mixed these before but now it seems like the second parameter replaces all tags. I really need it to add because I've got a string generated (which comes with inline attributes) and I need to add to it.

Here's [fiddle] for anyone who'd like to play with it.

Using IE9 and it breaks with:

SCRIPT438: Object doesn't support property or method 'createDocumentFragment' jquery-1.5.1.js, line 5450 character 3


First, use $(document).ready(function(){ or the shorthand $(function(){

Secondly, remove the url attribute from the third img and add the slash before the image src:

var img = $('<img src="icon.next.gif" url="/" />');
$("body").append(img);
img = $('<img />', { src: "icon.next.gif", url: "/" });
$("body").append(img);
img = $('<img src="/icon.next.gif" />');
$("body").append(img);

As long as you're pulling from a CDN, you should always try to grab the latest version of jQuery before posting a debug question, as the issue may have been resolved in jQuery itself.

You're using 1.5.1. jQuery is on 1.6.2 now, fyi.

http://jsfiddle.net/x2qSz/2/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜