gpt4 book ai didi

javascript - 不懂 jQuery .attr() 参数

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:09 33 4
gpt4 key购买 nike

$newUser.addClass(newTweet.user).attr('data-user', newTweet.user).text('@' + newTweet.user + ': ');
var user = $(this).data('user');

第1行newTweet是一个对象,但是参数'data-user我没看懂。这在任何其他文件中都没有引用,user,那么 'data-' 前缀是否在执行某些操作?或者它是否以某种方式引用了第 2 行?或者我在这两种情况下都完全错了,我也不明白 .addClass() 方法在这种情况下是如何工作的。如何将对象的属性添加为类?

最佳答案

.attr() 函数向元素添加属性。您可以在这里阅读更多相关信息:http://api.jquery.com/attr/ .

data-user 是添加到元素的属性名称。它不提供任何操作,但用于存储数据。

The data-* attributes is used to store custom data private to the page or application.

The data-* attributes gives us the ability to embed custom data attributes on all HTML elements.

The stored (custom) data can then be used in the page's JavaScript to create a more engaging user experience (without any Ajax calls or server-side database queries).

The data-* attributes consist of two parts:

The attribute name should not contain any uppercase letters, and must be at least one character long after the prefix "data-" The attribute value can be any string

Note: Custom attributes prefixed with "data-" will be completely ignored by the user agent.

https://www.w3schools.com/tags/att_global_data.asp .

对于 .attr() 调用和 .addClass(),它都使用 newTweet.user 的值,这可能是一个字符串。

关于javascript - 不懂 jQuery .attr() 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55453395/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com