1
在JQuery中,將一組節點的數據屬性獲取到數組中的最佳方式是什麼?在JQuery中,將一組節點的數據屬性獲取到數組中的最佳方式是什麼?
我得到這個語法工作:
var ids = $("a.some_class").map(function(index, item) {return item.getAttribute('data-id');});
有沒有更簡單的語法這個?
謝謝
在JQuery中,將一組節點的數據屬性獲取到數組中的最佳方式是什麼?在JQuery中,將一組節點的數據屬性獲取到數組中的最佳方式是什麼?
我得到這個語法工作:
var ids = $("a.some_class").map(function(index, item) {return item.getAttribute('data-id');});
有沒有更簡單的語法這個?
謝謝
嘗試
var ids = $("a.some_class").map(function() {
return $(this).data('id'); //or return this.getAttribute('data-id');
}).get();
如果您使用或不使用.get(),數據如何不同?謝謝 –
@ZackXu'.get()'會將'.map()'返回的jQuery-wrapped對象轉換爲普通的JavaScript數組,因此您可以像處理一個簡單的數組一樣使用它。 – VisioN