Home  >  Article  >  Web Front-end  >  A brief discussion on a small issue that is easily overlooked in H5’s data-*

A brief discussion on a small issue that is easily overlooked in H5’s data-*

青灯夜游
青灯夜游forward
2018-10-27 15:44:432657browse

This article brings you a brief discussion of a small issue that is easily overlooked in H5's data-*. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

H5 adds the data-* attribute, which is very convenient

But the problem of lowercase is often ignored. H5 requires all attribute names to be lowercase, and the habit of camel case naming is broken

The test code is as follows:

<html>
<head>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btn2").click(function(){
    alert($("div").data("id"));
    alert($("div").data("Id"));
    alert($("div").data("otherId"));
    alert($("div").data("OtherId"));
    alert($("div").data("OTHERID"));
    var datas = $("div").data();
  });
});
</script>
</head>
<body>

<button id="btn2">alert</button>
<div  data-id="小写id" data-Id="大写ID" data-otherId="驼峰id" data-other-id="横线id"></div>
</body>
</html>

The value from alert is unexpected. After obtaining all the data values ​​through the data() method, you can see the following results:

Summary: Follow standard writing

1. data-* All characters must be lowercase.

2. Multiple words are separated by horizontal lines. For example, data-other-id => otherId will remove the horizontal lines and capitalize the first letter when reading the attribute.

The above is the detailed content of A brief discussion on a small issue that is easily overlooked in H5’s data-*. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete