有时候我会看到像
$('<img/>')
这样的结构.$(‘< img />‘)与$(‘img’)有什么不同?我在哪里可以阅读更多相关信息?
我试着看jQuery Selectors,但没有发现与这种格式有关.
解决方法
当传递一个看起来像HTML的字符串时,jQuery函数被重载以构造新的jQuery元素.从
docs:
If a string is passed as the parameter to $(),jQuery examines the
string to see if it looks like HTML (i.e.,it starts with<tag ... >
). If not,the string is interpreted as a selector expression,as explained above. But if the string appears to be an HTML snippet,jQuery attempts to create new DOM elements as described by the HTML. Then a jQuery object is created and returned that refers to these elements.