欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

jquery中如何获取id值方法的总结

程序员文章站 2022-04-11 23:42:43
...
html页面中一些元素都可以有个本页面唯一的id,下面给大家介绍利用jquery获取id值方法的总结。希望对大家学习jquery有所帮助。
<div id="product_shift_out_{m}"> </div>
<script language = "JavaScript" type="text/javascript">
$(document).ready(function(){
name = $('div').eq(0).attr('id');
alert(name)
});
</script>

eq(0)是取第一个jq元素。。。

eq(index)
匹配一个给定索引值的元素

Matches a single element by its index.
返回值
Element

参数
index (Number) : 从 0 开始计数

示例
查找第二行

获取不同id的值

<script src="js/jquery.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
var len = $("#group span").size();//获取span标签的个数
var arr = [];
for(var index = 0; index < len-1; index++){//创建一个数字数组
arr[index] = index;
}
$.each(arr, function(i){//循环得到不同的id的值
var idValue = $("#group span").eq(i).attr("id");
if(idValue != ''){
alert(idValue);
}
});
});
//-->
</script>
<span id="group">
<span id="0_1">aaa,
<span group_id="0_1" class="icon_close"> </span>
</span>
<span id="0_2">bbb,
<span group_id="0_2" class="icon_close"> </span>
</span>
<span id="0_3">ccc,
<span group_id="0_3" class="icon_close"> </span>
</span>
<span id="0_4">ddd,
<span group_id="0_4" class="icon_close"> </span>
</span>
<span id="0_5">eee,
<span group_id="0_5" class="icon_close"> </span>
</span>
</span>

这样就会得到你想要的所有的id:

0_1
0_2
0_3
0_4
0_5

文本框,文本区域代码:

$("#txt").attr("value",'');//清空内容
$("#txt").attr("value",'11');//填充内容

多选框checkbox代码:

$("#chk1").attr("checked",'');//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined) //判断是否已经打勾

以上就是jquery中如何获取id值方法的总结的详细内容,更多请关注其它相关文章!