是否可以在 HTML 标记中插入您的属性
Is it possible to insert your attribute in the HTML-tag?
是否可以在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" group="3" onClick="myclick();"><br>
<script>
function myclick() {
alert(this.group);
}
</script>
最好使用 W3C 标准属性数据-*阅读有关数据属性的更多信息
function myclick(control)
{
alert($(control).attr("data-group"));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
first <input type="radio" id="qwerty1" data-group="1" onClick="myclick(this);"><br>
second <input type="radio" id="qwerty2" data-group="2" onClick="myclick(this);"><br>
third <input type="radio" id="qwerty3" data-group="3" onClick="myclick(this);"><br>
使用 jQuery 的 data() 函数进行更新
function myclick(control)
{
alert($(control).data("group"));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
first <input type="radio" id="qwerty1" data-group="1" onClick="myclick(this);"><br>
second <input type="radio" id="qwerty2" data-group="2" onClick="myclick(this);"><br>
third <input type="radio" id="qwerty3" data-group="3" onClick="myclick(this);"><br>
对普通 JS 使用getAttribute()
方法。
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,
使用.attr()
如果要使用用户定义的属性,可以使用数据属性。像这样:
<input type="radio" id="qwerty3" data-group="3" onClick="myclick(this);">
然后,要调用此值,您可以使用 .data() 函数访问它,如下所示。
$(function(){
function myclick(el)
{
alert($(el).data('group'));
}
});
最好使用data-*
属性作为W3C标准。
<input type="radio" id="qwerty1" data-group="1" onClick="myclick(this);"><br>
然后,您可以通过以下方式访问它:
$("#qwerty1").attr("data-group");
并通过以下方式设置:
$("#qwerty1").attr("data-group", "1");
您可以使用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>
您可以修改单击的处理程序以传递当前 dom 元素对象并使用它来获取与其关联的属性值:
<input type="radio" id="qwerty1" group="1" onClick="myclick(this);"><br>
.JS:
function myclick(obj){
alert($(obj).attr("group"));
}
是的,HTML标签中我们自己的属性是可能的。但为了安全起见,请在属性名称前使用"data-"前缀,以避免旧版浏览器出现任何问题。
first <input type="radio" id="qwerty1" data-group="1" onClick="myclick();">
试试这个(完整的Javascript方法):-
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) {
alert(elem.getAttribute('group'));
}
</script>
小提琴
旁注:- 不要以这种方式使用 HTML5 data-*
属性作为data-group="1"
,而不是使用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>
<script>
function myclick(elem) {
var value_gp = $(elem).attr("group");
alert(value_gp);
}
</script>
相关文章:
- 带有填充属性的SVG矩形显示在包含元素的上方插入框阴影
- 属性gridster项应插入到何处
- 手柄'img'单击事件并插入'alt'使用jQuery将属性转换为文本框
- 如何从动态属性将IMG插入DOM
- 选中复选框时插入名称属性
- 在主要的JavaScript引擎中,在JavaScript关联数组(动态对象属性)中检索/插入的复杂性是多少
- 是否可以在 HTML 标记中插入您的属性
- 扫描具有相同命名和插入值的所有数据属性-jQuery
- 为什么浏览器在此样式属性中插入“9”
- 在JavaScript中插入属性
- 如果我可以随时将任何属性插入对象,那么在javascript中创建类有什么用
- 如何在 Tinnymce 编辑器的插入链接弹出窗口中添加 rel 属性
- 在 XSLT 工作表中的 JavaScript 中插入属性的问题
- 环回 - 基于 id 以外的其他属性的 REST 更新插入/更新
- 如何在车把/Ember.js中插入动态{{属性}}
- 纯Javascript将文本插入中,不带属性
- 动态插入文件时触发 onload 属性.js
- 在数据库的1个属性中插入了多个值
- jQuery/Javascript向元素插入属性
- 使用javascript xml解析器在XMLnode中插入属性