我想在 id attr 中添加 id 屬性和唯一 id。我試過但在 id 屬性中獲取物件
$("div .abc").each(function() {
$('.abc').attr('id', $(this).uniqueId());
})
[id]::before {
content: attr(id);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
<div class="xyz">
<div class="abc"></div>
<div class="abc"></div>
<div class="abc"></div>
</div>
uj5u.com熱心網友回復:
jQueryUI.uniqueId()方法在內部完成您需要的所有作業。它回傳一個 jQuery 物件,這就是為什么你會看到看起來像一個物件的東西。您在.each()回呼中所需要的只是
$(this).uniqueId();
事實上,你甚至不需要.each():
$("div .abc").uniqueId();
將遍歷匹配的元素并給每個元素一個唯一的 id 值。
uj5u.com熱心網友回復:
完整代碼在這里
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="xyz">
<div class="abc"></div>
<div class="abc"></div>
<div class="abc"></div>
</div>
</body>
<script>
$(".abc").each(function (index) {
$(this).attr("id", "abc" index);
});
</script>
</html>
uj5u.com熱心網友回復:
您可以使用每個回圈或 for 回圈自動將 id 增加到 html 元素
每個回圈都是這樣;
$(".abc").each(function(i) {
$(".abc").attr('id', 'id' i);
})
或使用 for 回圈
for(var i = 0, i = $(".abc").length, i ){
$(".abc").attr('id', 'id' i);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359212.html
