Javascript 是否可以在HTML标记中插入属性?
是否可以在HTML标记中插入属性,然后引用它 例如,添加属性组:Javascript 是否可以在HTML标记中插入属性?,javascript,jquery,html,Javascript,Jquery,Html,是否可以在HTML标记中插入属性,然后引用它 例如,添加属性组: first <input type="radio" id="qwerty1" group="1" onClick="myclick();"><br> second <input type="radio" id="qwerty2" group="2" onClick="myclick();"><br> third <input type="radio" id="qwerty3"
first <input type="radio" id="qwerty1" group="1" onClick="myclick();"><br>
second <input type="radio" id="qwerty2" group="2" onClick="myclick();"><br>
third <input type="radio" id="qwerty3" group="3" onClick="myclick();"><br>
<script>
function myclick() {
alert(this.group);
}
</script>
首先
第二个
第三个
函数myclick(){
警报(本组);
}
您可以修改单击的处理程序以传递当前dom元素对象,并使用它获取与之关联的属性值:
<input type="radio" id="qwerty1" group="1" onClick="myclick(this);"><br>
是的,我们可以在HTML标签中定义自己的属性。但为了安全起见,请在属性名之前使用“data-”前缀,以避免旧浏览器出现任何问题
first <input type="radio" id="qwerty1" data-group="1" onClick="myclick();">
首先
试试这个(完整的Javascript方法):-
首先
第二个
第三个
函数myclick(elem){
警报(elem.getAttribute('group'));
}
旁注:-使用HTML5data-*
属性作为data group=“1”
使用普通JS的方法,而不是使用group
属性
function myclick(input) {
alert(input.getAttribute("group"));
}
此外,还需要通过函数调用传递当前对象
first <input type="radio" id="qwerty1" group="1" onClick="myclick(this);"><br>
second <input type="radio" id="qwerty2" group="2" onClick="myclick(this);"><br>
third <input type="radio" id="qwerty3" group="3" onClick="myclick(this);"><br>
首先
第二个
第三个
如果已经使用jQuery
使用如果要使用用户定义的属性,可以使用数据属性。比如:
<input type="radio" id="qwerty3" data-group="3" onClick="myclick(this);">
是的,您可以获得如下价值:-
first <input type="radio" id="qwerty1" group="1" onClick="myclick(this);"><br>
second <input type="radio" id="qwerty2" group="2" onClick="myclick(this);"><br>
third <input type="radio" id="qwerty3" group="3" onClick="myclick(this);"><br>
<script>
function myclick(elem) {
var value_gp = $(elem).attr("group");
alert(value_gp);
}
</script>
首先
第二个
第三个
函数myclick(elem){
var值_gp=$(elem).attr(“集团”);
警报(值_gp);
}
最好使用数据-*
属性作为W3C标准
<input type="radio" id="qwerty1" data-group="1" onClick="myclick(this);"><br>
并将其设置为:
$("#qwerty1").attr("data-group", "1");
最好使用W3C标准属性数据-*
函数myclick(控件)
{
警报($(控制).attr(“数据组”);
}
首先
第二个
第三个
您可以使用jquery单键单击功能来获取属性值,而不是每个单选按钮单击功能
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("input[name='radio1']").click(function(){
var value = $(this).attr("data-group");
alert(value);
});
});
</script>
<style>
.ra_label
{
cursor:pointer;
}
</style>
</head>
<body>
<label for="qwerty1" class="ra_label">first <input type="radio" id="qwerty1" name="radio1" data-group="1" ></label><br>
<label for="qwerty2" class="ra_label">second <input type="radio" id="qwerty2" name="radio1" data-group="2" ></label><br>
<label for="qwerty3" class="ra_label">third <input type="radio" id="qwerty3" name="radio1" data-group="3" ></label><br>
</body>
</html>
$(文档).ready(函数(){
$(“输入[name='radio1']”)。单击(函数(){
var值=$(this.attr(“数据组”);
警报(值);
});
});
.ra_标签
{
光标:指针;
}
首先
第二个
第三个
这是一种常见的模式,但通常数据的自定义属性都有前缀:data group=“1”在使用jQuery时,您还可以使用.data()
-函数来获取自定义数据属性这是一个很好的模式,我不知道.data()
-函数,谢谢您提供的信息。
<input type="radio" id="qwerty1" data-group="1" onClick="myclick(this);"><br>
$("#qwerty1").attr("data-group");
$("#qwerty1").attr("data-group", "1");
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("input[name='radio1']").click(function(){
var value = $(this).attr("data-group");
alert(value);
});
});
</script>
<style>
.ra_label
{
cursor:pointer;
}
</style>
</head>
<body>
<label for="qwerty1" class="ra_label">first <input type="radio" id="qwerty1" name="radio1" data-group="1" ></label><br>
<label for="qwerty2" class="ra_label">second <input type="radio" id="qwerty2" name="radio1" data-group="2" ></label><br>
<label for="qwerty3" class="ra_label">third <input type="radio" id="qwerty3" name="radio1" data-group="3" ></label><br>
</body>
</html>