jQuery追加字符串vs包含字符串的对象 [英] jQuery Append String vs Object Containing String

查看:177
本文介绍了jQuery追加字符串vs包含字符串的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这两种方法都会将html插入到元素中.

I understand that both of these will insert html into an element.

这两种方法之间有什么区别?

What are the differences between these 2 methods though?

字符串:

$("div").append("<h1>Header</h1>");

对象:

$("div").append($("<h1>Header</h1>"));

仅仅是您可以做这样的事情吗?

Is it just that you can do stuff like this?

$("div").append($("<h1></h1>").html("Header"));

推荐答案

仅仅是您可以做这样的事情吗?

Is it just that you can do stuff like this?

$("div").append($("<h1></h1>").html("Header"));

完全

通过这种方式为您创建一个jQuery对象,可以使用诸如append,css,addClass之类的功能对其进行进一步的操作

Doing it this way creates for you a jQuery object which can further be manipulated with functions like append, css, addClass

$("div").append($("<h1></h1>").html("Header")
                              .addClass("someClass")
                              .css("color", "gree"));

或者,如果您不想做这样的进一步操作,则一定要将它传递给一个字符串,您将得到相同的结果.

Or if you don't want to do further manipulation like this, then by all means pass it just a string, and you'll get the same result.

这篇关于jQuery追加字符串vs包含字符串的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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