思路:利用jQuery选择器直接根据类名获取对象→遍历对象→使用 attr() 方法获取id属性值。下面实例演示——获取所有class为test的元素的id值:
1、HTML结构
<div class="test" id="div_id">我的类是test,id是div_id</div><input type="text" class="test" id="input_id" value="我的类是test,id是input_id" />
<div class="test">我的类是test,没有id属性</div>
<li class="test" id="li_id">我的类是test,id是li_id</li>
<input type="button" value="确定">
2、jQuery代码
$(function(){$("input[type='button']")click(function() {
var ids = [];
$("test")each(function(index, el) {
var id = $(this)attr("id");
if(typeof(id)!=='undefined'){
idspush(id)
}
});
alert(ids);
});
});
3、效果演示
// 找最新的父元素tr
// 再找第一个div
// 获取id
$('#1875')closest('tr')find('div:eq(0)')attr('id')
使用 each 方法, 假设你的 jQuer 元素是 el , 大概代码是:
$(el)each(function(index,el){
consolelog( $(el)attr('id') );
})
each 是 jQuery 提供的遍历方法,第一个参数是索引,第二个参数是遍历对象的值。
以上就是关于jquery怎样在获取同一个div下的类的ID全部的内容,包括:jquery怎样在获取同一个div下的类的ID、jquery如何动态获取div的id、如何遍历jquery元素获取所有id等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)