简体   繁体   English

我可以在HTML选择框元素中使用html5数据属性吗?

[英]Can I use html5 data attribute in HTML select box element?

Any idea can I use .data attribute in HTML select box?I red HTML5 documents but I didn't find any information that could help me. 我可以在HTML选择框中使用.data属性吗?我将HTML5文档设为红色,但是没有找到任何可以帮助我的信息。

Is it legal: 是否合法:

<span>Select depatament</span>
<span>
    <select id="department" onchange="EnableSelectBox(this)" data-spacing="10cm">
        <option selected disabled>-Select-</option>
    </select>
</span>

Using data attributes 使用数据属性

Any attribute on any element whose attribute name starts with data- is a data attribute. 属性名称以data-开头的任何元素上的任何属性都是数据属性。

To answer the question: Yes. 要回答这个问题:是的。 You can use data attributes on any element. 您可以在任何元素上使用数据属性。

Yes it fine to use data in Html5 tag. 是的,可以在Html5标签中使用数据。 I don't know why you want to use in such a way, but you can use it. 我不知道您为什么要以这种方式使用,但是您可以使用它。

for example like this 例如这样

$(document).ready(function() { alert($('#department').data('spacing')); });

jsfiddle link jsfiddle链接

It works fine. 工作正常。 Test in JS using: 使用以下方法在JS中进行测试:

alert(document.getElementById("department").getAttribute("data-spacing"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM