var noticeFile = "";
$("a[flag='deleteFile']").each(function(){
noticeFile += $(this).attr("id") + ",";
});
| 代碼 | 含義 |
|---|---|
$("a[flag='deleteFile']") | 選取**所有** <a> 元素,其屬性 flag 值為 'deleteFile'。返回 jQuery 物件 (類陣列)。 |
.each(function(){...}) | 遍歷上述集合,**每個元素**執行一次回調函數。回調內 this 為原生 DOM 元素。 |
$(this) | 將原生 DOM 包裝為 jQuery 物件,以使用 jQuery 方法。 |
.attr("id") | 獲取該元素的 id 屬性值 (字串)。 |
noticeFile += ... + "," | 將 id 附加到字串變數,逗號分隔。 |
HTML:
刪除1
刪除2
其他
noticeFile 最終為 "f1,f2," (注意結尾有逗號)。
// 移除尾隨逗號
var ids = $("a[flag='deleteFile']").map(function(){ return this.id; }).get().join(",");
// ids = "f1,f2"
// 或使用現代 JS (無 jQuery 依賴)
const ids = [...document.querySelectorAll("a[flag='deleteFile']")].map(e => e.id).join(",");