javascript 使用 .get(0) 或 .html() 通过 jQuery 返回 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8160585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:29:36  来源:igfitidea点击:

Use .get(0) or .html() to return HTML with jQuery

javascriptjqueryhtml

提问by OldDrunkenSailor

So as far as I can tell, if I have

据我所知,如果我有

<div id="thing">OMG there's awesome stuff in here</div>

and need to plant that html somewhere else, I have the option of using:
$('#thing').html();or $('#thing').get(0);

并且需要将该 html 植入其他地方,我可以选择使用:
$('#thing').html();$('#thing').get(0);

Is there a greater internet standard in using one or the other? They do the exact same thing, right?

是否有更高的互联网标准使用其中之一?他们做完全一样的事情,对吧?

Thanks for the help!

谢谢您的帮助!

回答by James Allardice

They do the exact same thing, right?

他们做完全一样的事情,对吧?

Wrong. The htmlmethod returns the contentsof the selected element, as a string. The getmethod returns the element itself, as an object. For example, we have this element:

错误的。该html方法以字符串形式返回所选元素的内容。该get方法返回元素本身,作为一个对象。例如,我们有这个元素:

<div id="example">
    <span>A child element</span>
</div>

The methods would return the following:

这些方法将返回以下内容:

console.log($("#example").html()); //Prints "<span>A child element</span>"
console.log($("#example").get(0)); //Prints an object

You can try that for yourself here.

您可以在这里亲自尝试。

回答by James Montagne

.get(0)will give you the first element in the jquery object, not the HTML within that. You would then need to get the html. If you're using jquery, use jquery. I see no reason not to use .html().

.get(0)将为您提供 jquery 对象中的第一个元素,而不是其中的 HTML。然后,您需要获取 html。如果您使用 jquery,请使用 jquery。我认为没有理由不使用.html().

回答by jfriend00

These two things do close to the same thing (note the addition of .innerHTMLto what you had in your question):

这两件事与同一件事很接近(请注意在问题中添加的.innerHTML内容):

$('#thing').html();

$('#thing').get(0).innerHTML;

The first creates a jQuery object, then calls the .html() method on it (which in turns gets the HTML from the .innerHTMLproperty).

第一个创建一个 jQuery 对象,然后在它上面调用 .html() 方法(它依次从.innerHTML属性中获取 HTML )。

The second creates a jQuery object, then gets the first DOM element out of it and gets the innerHTML property from it.

第二个创建一个 jQuery 对象,然后从中获取第一个 DOM 元素并从中获取 innerHTML 属性。

In general, if you already have a jQuery object, then use .html(). If you already have a DOM object, then use .innerHTML.

一般来说,如果你已经有一个 jQuery 对象,那么使用.html(). 如果你已经有一个 DOM 对象,那么使用.innerHTML.

回答by lonesomeday

If you want to duplicate some elements, do notuse html. It is a very inefficient way of cloning elements. There is a much better way, called clone(funnily enough):

如果要复制某些元素,请不要使用html. 这是一种非常低效的克隆元素的方式。有一种更好的方法,称为clone(很有趣):

$('#thing').clone(true).removeProp('id').appendTo('#someOtherElement');

Note that I am removing the idproperty, as it must be unique.

请注意,我正在删除该id属性,因为它必须是唯一的。

回答by JAAulde

No, they do not do the exact same thing.

不,他们不做完全相同的事情。

.html()returns HTML string representing the DOM structure insidethe element contained within the jQuery collection object: "OMG there's awesome stuff in here".

.html()代表DOM结构返回HTML字符串“OMG有真棒东西在这里”:包含了jQuery集合对象中的元素。

.get(0)returns the first DOM element object from the collection, in this case a DOM node of tag-type DIVwith ID "thing" and child text-node with value "OMG there's awesome stuff in here"

.get(0)从集合中返回第一个 DOM 元素对象,在这种情况下,一个标签类型的 DOM 节点,DIVID 为“thing”,子文本节点的值为“OMG 这里有很棒的东西”

回答by Kai Qing

I've only used .html() and I work for a firm. I have never seen .get(0) used for anything like this

我只使用过 .html() 并且我在一家公司工作。我从未见过 .get(0) 用于这样的事情

回答by JaredPar

They are fairly different. The getmethod returns a DOM element while html()returns a string. Here it looks like you want the HTML so use html()

它们相当不同。该get方法返回一个 DOM 元素,同时html()返回一个字符串。这里看起来你想要 HTML 所以使用html()

回答by mikeycgto

These two methods are different

这两种方法不同

get()Retrieve the DOM elements matched by the jQuery object. See: http://api.jquery.com/get/

get()检索与 jQuery 对象匹配的 DOM 元素。见:http: //api.jquery.com/get/

html()Get the HTML contents of the first element in the set of matched elements. See: http://api.jquery.com/html/

html()获取匹配元素集中第一个元素的 HTML 内容。见:http: //api.jquery.com/html/