HTML自定义属性在Chrome中不起作用 [英] HTML Custom Attributes Not Working in Chrome

查看:211
本文介绍了HTML自定义属性在Chrome中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用HTML自定义属性时,它无法在Chrome中使用。

我的意思是,假设我有这样的HTML:

What I mean is, suppose I have this HTML:

<div id="my_div" my_attr="1"></div>

如果我尝试在Chrome中使用JavaScript获取此属性,我会 undefined

If I try to get this attribute with JavaScript in Chrome, I get undefined

alert( document.getElementById( "my_div" ).my_attr );

在IE中它运作正常。

推荐答案

通过getAttribute()检索它:

Retrieving it via getAttribute():

alert(document.getElementById( "my_div" ).getAttribute("my_attr"));

在IE,FF和Chrome上工作正常。

Works fine for me across IE, FF and Chrome.

这篇关于HTML自定义属性在Chrome中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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