Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以使用jQuery方法attr()设置元素的ID和类?_Javascript_Jquery_Class_Attr - Fatal编程技术网

Javascript 是否可以使用jQuery方法attr()设置元素的ID和类?

Javascript 是否可以使用jQuery方法attr()设置元素的ID和类?,javascript,jquery,class,attr,Javascript,Jquery,Class,Attr,是否可以使用上面的设置元素的ID或类。hm我觉得我已经说过数千次了。jQuery类选择器返回该类中所有HTML元素的数组。因此,如果要从这些元素获取数据,需要在数组上循环或直接访问它 $("selector").attr("id","idname"); 在你的代码中你正在设置属性?!你到底想做什么。如果您想获得id属性,只需编写$.class.attrid;使用find方法jqueryy您的代码可以设置ID属性,但不幸的是,您的问题是稀疏和懒惰的。我在问它是对的还是错的? $('.gg1').

是否可以使用上面的设置元素的ID或类。

hm我觉得我已经说过数千次了。jQuery类选择器返回该类中所有HTML元素的数组。因此,如果要从这些元素获取数据,需要在数组上循环或直接访问它

$("selector").attr("id","idname");

在你的代码中你正在设置属性?!你到底想做什么。如果您想获得id属性,只需编写$.class.attrid;使用find方法jqueryy您的代码可以设置ID属性,但不幸的是,您的问题是稀疏和懒惰的。我在问它是对的还是错的?
$('.gg1').first().attr("id"); //returns the id of the first element with class gg1
$('.gg1')[1].attr("id"); //returns the id of the second element with class gg1

for(var i = 0; i < $('.gg1').legth; i++= {
  console.log($('.gg1')[i]); } //loops over the cormplete HTML Collection