Javascript 如何在jquery中为元素设置id?
基本上,我使用jqueryJavascript 如何在jquery中为元素设置id?,javascript,jquery,html,Javascript,Jquery,Html,基本上,我使用jqueryappend()函数创建了x个图像框,所有这些框都具有相同的类名,但现在我需要将每个框id设置为I。为了避免混淆,我的意思是: for(i=0;i<10;i++){ $('.d').append('<img src="" class="UP__" width="120" height="120">'); $('.d img').attr("id",i); } for(i=0;i只需将id添加到img标签 $('.d')。附加('');
append()
函数创建了x个图像框,所有这些框都具有相同的类名,但现在我需要将每个框id设置为I
。为了避免混淆,我的意思是:
for(i=0;i<10;i++){
$('.d').append('<img src="" class="UP__" width="120" height="120">');
$('.d img').attr("id",i);
}
for(i=0;i只需将id添加到img标签
$('.d')。附加('');
只需将id添加到img标记
$('.d')。附加('');
首先声明变量(无论是使用var
还是let
),以避免创建全局变量,然后可以在循环中添加id
:
for(let i=0; i<10; i++){
$('.d').append('<img src="" id="' + i + '" class="UP__" width="120" height="120">');
}
for(let i=0;i<10;i++){
$('.d').append('<img src="" class="UP__" width="120" height="120">');
}
// here we select all the matching <img> elements, and update
// 'id' property using the prop() method, and the anonymous
// function:
$('.d img').prop('id', function (index){
// 'index' is the index of the current element in
// the jQuery collection over which prop() iterates:
return index;
});
首先声明变量(无论是使用var
还是let
),以避免创建全局变量,然后可以在循环中添加id
:
for(let i=0; i<10; i++){
$('.d').append('<img src="" id="' + i + '" class="UP__" width="120" height="120">');
}
for(let i=0;i<10;i++){
$('.d').append('<img src="" class="UP__" width="120" height="120">');
}
// here we select all the matching <img> elements, and update
// 'id' property using the prop() method, and the anonymous
// function:
$('.d img').prop('id', function (index){
// 'index' is the index of the current element in
// the jQuery collection over which prop() iterates:
return index;
});
您不需要提供ID。只需使用jQuery的每个函数即可
我手动添加了图像,但您当然可以使用自己的代码使其动态化
<div class="d">
<img src="http://afterishtar.pl/images/100x100.gif">
<img src="http://afterishtar.pl/images/100x100.gif">
<img src="http://afterishtar.pl/images/100x100.gif">
<img src="http://afterishtar.pl/images/100x100.gif">
<img src="http://afterishtar.pl/images/100x100.gif">
</div>
您不需要提供ID。只需使用jQuery的每个函数即可
我手动添加了图像,但您当然可以使用自己的代码使其动态化
<div class="d">
<img src="http://afterishtar.pl/images/100x100.gif">
<img src="http://afterishtar.pl/images/100x100.gif">
<img src="http://afterishtar.pl/images/100x100.gif">
<img src="http://afterishtar.pl/images/100x100.gif">
<img src="http://afterishtar.pl/images/100x100.gif">
</div>
这是因为您正在向.d div中的所有img标记添加id
PS:始终正确设置图像src标记。空的图像src标记会产生很多问题,而不是解决问题。如果src未知或稍后设置,请使用about_blank
另外,最好尽可能少地更改dom。如果可能,在所有操作完成后,在将新创建的元素附加到dom之前,附加这些元素
$(函数(){
对于(i=0;i<10;i++){
var$img=$('');
$img.attr(“id”,i).attr(“title”,i);
$('.d')。追加($img);
}
});
这是因为您正在将id添加到.d div中的所有img标记中
PS:始终正确设置图像src标记。空的图像src标记会产生很多问题,而不是解决问题。如果src未知或稍后设置,请使用about_blank
另外,最好尽可能少地更改dom。如果可能,在所有操作完成后,在将新创建的元素附加到dom之前,附加这些元素
$(函数(){
对于(i=0;i<10;i++){
var$img=$('');
$img.attr(“id”,i).attr(“title”,i);
$('.d')。追加($img);
}
});
在append方法中,您可以使用类似于:$('.d')。append('');ID不应该是数字。执行类似于i1
,i2
…的操作,以便为将来的用户演示更新,以便在append方法中显示ID。您可以使用类似于:$('.d')。append('');ID不应该是数字。请执行类似于i1
,i2
…为将来的用户进行演示,以更新显示ID