如何在对象属性名称中使用连字符? [英] How to use hyphen in object property name?

查看:62
本文介绍了如何在对象属性名称中使用连字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$('.class').data({data-toggle:"whatever-value",data-target:"#id"});

我不断收到-

我最初尝试使用 .attr ,然后切换到 .data ,因为我认为这样可以解决该问题.

I originally tried .attr then switched to .data because I thought that would fix it.

有人可以帮助解决这个简单的问题吗?

Can someone help with this easy problem?

谢谢

推荐答案

如果要设置/更新 data-* 属性,则在设置属性时需要加引号:

If you want to set/update a data-* attribute, you need quotes if you set the attribute:

$('.class').attr({"data-toggle": "whatever-value", "data-target": "#id"});

否则它将像

data-toggle

含义

data - toggle

减去两个变量.

您还可以将 .data()

$('.class').data({"toggle": "whatever-value", "target": "#id"});

但不分配 data-* 属性,它只是将数据存储在jQuery的存储系统中.

but that does not assign data-* attributes it just stores the data in jQuery's storage system.

这篇关于如何在对象属性名称中使用连字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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