附加函数不返回附加对象吗? [英] Doesn't the append function return the appended object?

查看:67
本文介绍了附加函数不返回附加对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设this是dom中一些现有的块级元素,而imagehttp://www.google.com/images/srpr/nav_logo25.png,则执行以下操作不起作用:

Assuming this is some existing block level element in the dom and image is http://www.google.com/images/srpr/nav_logo25.png doing the following does not work:

$(this).append('<img>').attr('src', image).load(function() { alert('loaded'); });

应该按照以下步骤进行吗?:

Should it be done as follows?:

$(this).append($('<img>').attr('src', opts.image).load(function() { alert('loaded'); }));

还是应该以其他方式完成?

Or it should be done some other way?

推荐答案

.append() 返回您要附加的对象,要获取要附加到链中的对象,请改用 .appendTo() :

$('<img>').appendTo(this).load(function() { alert('loaded'); })
                         .attr('src', opts.image);

两个版本都返回以前链中的内容...但是由于要处理<img>,请使用.appendTo()形式.另外,为了获得可靠的负载结果,请像上面一样设置src之前,先连接负载处理程序 .

Both versions return what was in the chain previously...but since you want to deal with the <img> use the .appendTo() form. Also, for reliable results with your load, attach your load handler before setting the src as I have above.

这篇关于附加函数不返回附加对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆