尝试通过参加 Code School CoffeeScript 类(class)来掌握 CoffeeScript 和 jQuery。
使用的摘录之一是 $("<li>" + name + "</li>")
。我设法弄清楚$
是 jQuery
的别名(对吗?),所以我想这意味着我们正在使用字符串调用 jQuery 函数( name
是一个字符串,被两个文字包围)。
那么... jQuery
是什么意思?函数自己做?尝试查看 api.jquery.com,但无法弄清楚。谢谢!
最佳答案
在本例中,您将使用 jQuery 创建 DOM 元素。
安<li>
变量 name
中的一些(我假设的文本)
If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML (i.e., it has somewhere within the string). 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.
使用 jQuery 创建 DOM 元素的结构是:
$( html, props )
看看这些 jQuery Docs 阅读有关 jQuery 选择器的更多信息。
html: A string defining a single, standalone, HTML element (e.g. or ).
props: A map of attributes, events, and methods to call on the newly-created element.
正如我们在示例中看到的,我们仅传递 html。 $("<li>" + name + "</li>")
关于jquery - 将字符串传递给 $,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14142303/