为什么这个jQuery引用'$("< div />")'而不是'$("< div>< / div>")''? [英] Why is this jQuery reference '$("<div />")' instead of '$("<div></div>")'?
问题描述
popup =
$("<div />")
.css(settings.popupCSS)
.attr("id", settings.popupId)
.css("position", "absolute")
.appendTo("body").hide();
我正在阅读一些jQuery代码,我对<$ c $的内容感到有点困惑c> $(< div />)表示。它只是指那个时刻突然出现的< div />
实例?
I'm reading some jQuery code and I'm a bit confused as to what $("<div />")
means. Is it just referring to the <div />
instance that's popping up at that moment?
推荐答案
从技术上讲,如果你使用 $('< div />')
或 $(无) '< div>< / div>')
。
Technically it doesn't matter if you use $('<div />')
or $('<div></div>')
.
这段代码正在做的是创建一个新的div元素,添加一些css样式,添加id,定位,将其附加到身体然后隐藏它。
What this code is doing is creating a new div element, adding some css styles to it, adding an id, positioning it, appending it to the body and then hiding it.
我猜jQuery使用document.createElement来创建元素,这意味着浏览器知道如何渲染它。
I'm guessing jQuery uses document.createElement to create the element and that means the browser knows how to render it.
这篇关于为什么这个jQuery引用'$("< div />")'而不是'$("< div>< / div>")''?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!