jquery - 将字符串传递给 $

标签 jquery

尝试通过参加 Code School CoffeeScript 类(class)来掌握 CoffeeScriptjQuery

使用的摘录之一是 $("<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/

相关文章:

jquery - 安装 JQuery 时,为什么我的 "delete"链接断开?

javascript - 在 jquery 选择器中使用变量

javascript - Fancybox - 根据 id 从元素获取 html?

Javascript 在函数调用时终止

javascript - Rails javascript 仅适用于双击,如何解决?

Jquery 和change() 具有超过1 个函数?

Jquery Mobile 弹出窗口在第一次单击时未居中

javascript - 在右边隐藏可变宽度元素

javascript - 不能在 JQuery 中使用创建的函数

javascript - 您的 Rails 应用程序如何包含 JavaScript?