jquery - 是否可以使用 JS 变量通过 jQuery attr() 函数设置自定义属性

标签 jquery custom-attributes

我正在使用 jquery 1.5 和 html4 标准。
我正在尝试设置通过 javascript 变量获取的自定义属性,但它没有设置。代码示例:

var attname="list1"; // this is changed on every call of the function where it is defined.    
var attvalue="b,c,d"; // this also changed.  
jQuery('#div1').attr({attname:attvalue});

但它将 attname 视为字符串本身而不是变量。 还有其他选项,例如使用 data()、prop(),但它们在 HTML5 和 jquery 1.6 中受支持,目前对我来说是不可能的。另一个问题是无法在服务器端设置数据以进行同步和使用客户端通过jquery data()。因为它们在语法上是不同的。 如果还有其他方法请建议 谢谢。

最佳答案

我想你应该使用这个

jQuery('#div').attr(attname,attvalue);

关于jquery - 是否可以使用 JS 变量通过 jQuery attr() 函数设置自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7497066/

相关文章:

jquery - TYPO3 extbase - jquery 的流体变量

javascript - "this"在 jQuery 中是如何工作的

c# 4.0 使用(指定的属性和 attribute.data)获取类属性

c# - 使用 CustomAttributes 与 GetCustomAttributes() 的优势

c# - 将信息传递给应用了 SoapExtensionAttribute 的对象

attributes - AllowMultiple不能与属性属性一起使用?

javascript - setInterval 图像未立即加载的问题

php - 我可以将 Logo 移到顶部菜单的中央吗?

c# - 将多个属性合并为一个属性

jquery - 用图片替换复选框 asp.net