如何獲取每個列表索引的id?從div元素獲取列表索引id
調試的螢火顯示:
<li class="droptrue ui-draggable" style="display: list-item;">
"...text of the li..."
</li>
我傳遞一個唯一的ID給每個列表元素。 下面是一個按比例縮小樣品中的jsfiddle:
http://jsfiddle.net/n2learning/tV4n7/89/
謝謝!
如何獲取每個列表索引的id?從div元素獲取列表索引id
調試的螢火顯示:
<li class="droptrue ui-draggable" style="display: list-item;">
"...text of the li..."
</li>
我傳遞一個唯一的ID給每個列表元素。 下面是一個按比例縮小樣品中的jsfiddle:
http://jsfiddle.net/n2learning/tV4n7/89/
謝謝!
id
屬性不會被克隆。所以改成別的東西data-id
可以解決你的問題。
$('ul#routinefilter li').each(function() {
var id = $(this).attr('id');
});
您有語法錯誤。缺少關閉');'。 – 2012-03-04 19:17:23
@Jey Balachandran,謝謝:) – dotoree 2012-03-04 19:18:18
要創建具有所有的ID的數組:
var ids = [];
$('#routinefilter li').each(function() {
ids.push(this.id);
});
或者,也許更容易在以後使用,重視數據()
$('#routinefilter li').each(function() {
$(this).data('id', this.id);
});
的數據屬性似乎堅持,這裏的a FIDDLE
使用數據而不是ID!
爲您的draggable
添加回調。
$(".droptrue").draggable({
snap: '#dropTargetframe',
connectToSortable: '#dropTargetframe',
helper: 'clone',
cursor: 'move',
revert: false,
drag: function (event, ui) {
var id = $(this).attr('id');
console.log(id);
}
});
感謝您的意見。我將如何引用javascript或jQuery中的數據ID?我仍然會使用$(this).attr('id');? – Derek 2012-03-04 19:42:06
$(this).attr('data-id') – 2012-03-05 01:24:33