我可以像下面这样向HTML标记添加自定义属性吗?
<tag myAttri="myVal" />
我可以像下面这样向HTML标记添加自定义属性吗?
<tag myAttri="myVal" />
当前回答
你可以像这样从JavaScript中提取你想要的值,而不是一个属性:
<a href='#' class='click'>
<span style='display:none;'>value for JavaScript</span>some text
</a>
其他回答
不,这将破坏验证。
在HTML 5中,您可以/将能够添加自定义属性。就像这样:
<tag data-myAttri="myVal" />
另一种干净的方法是将你想要的数据连接到另一个标签中,例如id,然后使用split来获取你想要的东西。
<html>
<script>
function demonstrate(){
var x = document.getElementById("example data").querySelectorAll("input");
console.log(x);
for(i=0;i<x.length;i++){
var line_to_illustrate = x[i].id + ":" + document.getElementById ( x[i].id ).value;
//concatenated values
console.log("this is all together: " + line_to_illustrate);
//split values
var split_line_to_illustrate = line_to_illustrate.split(":");
for(j=0;j<split_line_to_illustrate.length;j++){
console.log("item " + j+ " is: " + split_line_to_illustrate[j]);
}
}
}
</script>
<body>
<div id="example data">
<!-- consider the id values representing a 'from-to' relationship -->
<input id="1:2" type="number" name="quantity" min="0" max="9" value="2">
<input id="1:4" type="number" name="quantity" min="0" max="9" value="1">
<input id="3:6" type="number" name="quantity" min="0" max="9" value="5">
</div>
<input type="button" name="" id="?" value="show me" onclick="demonstrate()"/>
</body>
</html>
你可以像这样从JavaScript中提取你想要的值,而不是一个属性:
<a href='#' class='click'>
<span style='display:none;'>value for JavaScript</span>some text
</a>
是的,你可以,你在问题本身做到了:<html myAttri="myVal"/>。
使用任何数据,我经常使用它们
<aside data-area="asidetop" data-type="responsive" class="top">