将字符串传递给$(Passing a string to $)

编程入门 行业动态 更新时间:2024-10-28 17:24:05
字符串传递给$(Passing a string to $)

尝试通过Code School CoffeeScript课程掌握CoffeeScriptjQuery

使用的摘录之一是$("<li>" + name + "</li>") 。 我设法弄清楚$是jQuery的别名(对吧?),所以我猜这意味着我们用字符串调用jQuery函数( name是一个字符串,由两个文字包围)。

那么...... jQuery函数自己做了什么? 试着看着api.jquery.com,很难搞清楚。 谢谢!

Trying to get a grip on CoffeeScript and jQuery by doing the Code School CoffeeScript course.

One of the excerpts used is $("<li>" + name + "</li>"). I managed to figure out that $ is an alias for jQuery (right?), so I guess this means we're calling the jQuery function with a string (name is a string, surrounded by two literals).

So... what does the jQuery function do on its own? Tried looking at api.jquery.com, having trouble figuring it out. Thank you!

最满意答案

在这个例子中,您使用jQuery来创建DOM元素。

一个<li>与变量name中的一些(我假设的文本)

如果一个字符串作为参数传递给$() ,jQuery会检查字符串以查看它是否看起来像HTML(即它在字符串中的某个位置)。 如果不是,则将字符串解释为选择器表达式,如上所述。 但是如果字符串看起来像是一个HTML片段,那么jQuery会尝试按照HTML的描述创建新的DOM元素。 然后创建并返回引用这些元素的jQuery对象。

使用jQuery创建DOM元素的结构是:

$( html, props )

查看这些jQuery Docs以阅读有关jQuery选择器的更多信息。

html :定义单个独立HTML元素的字符串(例如或)。

props :要在新创建的元素上调用的属性,事件和方法的映射。

正如我们在您的示例中看到的,我们只传递html 。 $("<li>" + name + "</li>")

In this instance you are using jQuery to create a DOM element.

An <li> with some (text I'm assuming) that is in the variable 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.

The structure for creating DOM elements with jQuery is:

$( html, props )

Check out these jQuery Docs to read more about the jQuery selector.

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.

As we can see in your example we only pass html. $("<li>" + name + "</li>")

更多推荐

本文发布于:2023-07-25 22:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267134.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   Passing   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!