我是jQuery的新手.我浏览了这个使用< div />的jQuery代码.作为选择器:
$("<div />").html(someString).text();
首先,我想知道它是否是一个合法的标签,因为div有一个结束标记:< / div>.
但我的主要问题 – jQuery是否可以使用结束标记作为选择器,这些代码的结果是什么?
怎么做这样的代码?我做了一些研究,here,meagar说:
There is no such thing as a “closing tag” in the DOM. Tags,closing or otherwise,are a component of your markup,the DOM has only elements.
但jQuery是否将结束标记视为对DOM元素的引用,就像处理开始标记一样?
非常感谢你.
解决方法
看到这些:
> $(“div”)它是一个选择器,用于选择页面中的所有div.
> $(“< div>”)这会创建一个dom节点< div>< / div>因为< tags>.
> $(“< div />”)和$(“< div>< / div>”)也与2相同.
因此,在javascript中有一个名为document.createElement(tagName)的方法,该方法用于在页面中创建新元素.
所以基本上jQuery有不同的语法,但在幕后使用document.createElement(tagName).
你评论:
var div = $('<div/>').html('<p>Hi!!!</p>').text(); // put a html string and get the text content of it. document.body.textContent = div; // put the text content in the doc body.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>