jQuery - 将 div 附加到正文,正文是对象? [英] jQuery - Appending a div to body, the body is the object?

查看:27
本文介绍了jQuery - 将 div 附加到正文,正文是对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向 body 添加一个 div 时,它会将 body 作为对象返回,然后每当我使用它时 - 它显然是在使用 body.不好.

When I'm adding a div to body, it's returning the body as the object and then whenever I use that - it's obviously using body. Bad.

代码:-

var holdyDiv = $('body').append('div');
$(holdyDiv).attr('id', 'holdy');

holdy 的 'id' 现在被添加到 body 中......是吗?我做错了什么.

The 'id' of holdy is now being added to body... eh? What am I doing wrong.

推荐答案

jQuery 方法返回应用它们的集合.

jQuery methods returns the set they were applied on.

使用.appendTo:

var $div = $('<div />').appendTo('body');
$div.attr('id', 'holdy');

这篇关于jQuery - 将 div 附加到正文,正文是对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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