如何更改自定义属性的值 [英] How to change the value of a custom attribute

查看:61
本文介绍了如何更改自定义属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var selector = $(this);

selector更改自定义属性data-change-me的正确代码是什么?

What is the proper code to change the custom attribute data-change-me for selector?

语法

selector[data-change-me='someValue'];

不是为我工作

推荐答案

我认为您需要attr方法.

I think you want the attr method.

selector.attr("data-change-me","someValue");

这里是文档: http://api.jquery.com/attr/

值得指出的是,您试图将jQuery包装的DOM对象视为Javascript对象,例如:

It's worth pointing out also that it looked like what you were trying to do was treat the jQuery wrapped DOM object as a Javascript object e.g:

请看以下示例以了解我的意思:

Look at the following example for what I mean:

var myObject = {
     "data-change-me":"someValue";
};

myObject["data-change-me"] = "someOtherValue";

这是纯Javascript对象(而不是jQuery)的有效语法.要了解Javascript,我建议 Javascript的好部分

This is valid syntax for pure Javascript objects but not jQuery. To learn about Javascript I recommend Javascript the good parts

这篇关于如何更改自定义属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆