我正在開發一個使用javascript的應用程序。我需要的是使用id的div(1,2,3 ...),並且能夠在jquery之間插入div(例如2和3),然後讓它成爲新的三個,並且三個成爲四,四變爲五等。我有div插入工作,我只需要知道如何重新排列div。有任何想法嗎?當使用Javascript在中間添加一個div時,對數字順序的div ID進行重新編號
1
A
回答
3
後您插入新的div,你可以這樣做:
var i = 1;
$('div').each(function() {
$(this).attr('id', i++);
});
由你自己選擇替換$( '格')。
還請記住,根據您使用的HTML版本,id不能以數字開頭。
0
我很確定你從DOM選擇器返回DOM順序,所以你不能找到父元素,選擇子元素<div>
,然後.each()
通過列表?
1
你不能用一個數值開始ID,但不管你會做這樣的事情
// set a data value in the div you have just inserted into the dom and set a variable theID to the current ID you have just inserted.
$(this).data('inserted', true);
var theID = $(this).attr('id'); // this will be 3.
// now to update the other divs.
$('div').each(function() {
if($(this).attr('id') >= theID && !$(this).data('inserted')){
$(this).attr('id', $(this).attr('id') + 1);
}
});
// now set the data inserted to false for future updates
$('div#3').data('inserted', false);
1
$(function() {
reorder();
$('#click').click(function() {
$('<h2>hello world blah!</h2>').insertAfter('.content h2:eq(1)');
reorder();
});
});
function reorder() {
$('.content h2').each(function(i) {
$(this).attr('id', 'order_'+(i+1));
// alert(parseInt(this.id.split('_')[1])); // this is the id #
});
};
相關問題
- 1. 對順序ID進行重新排序
- 2. 添加一個數字序列的div
- 3. 當我在DIV標記中使用Javascript樣式時,添加了一箇中斷
- 4. JavaScript添加/刪除順序div(添加/刪除/插入/重新排序步驟)
- 5. 如何使用JavaScript在另一個div上添加div?
- 6. 一次對錶中的數字進行重新編號
- 7. 如何將div添加到使用javascript的另一個div?
- 8. 使用Javascript在單個div中添加多個輸入字段
- 9. JavaScript在Div標籤上動態添加另一個Div數據
- 10. 將div添加到div,每個div的數字增加
- 11. 使用javascript函數編號的div
- 12. 使用Javascript添加div的Javascript
- 13. 爲每個div添加一個數字
- 14. 如何在javascript數組中添加某個div元素的id?
- 15. 我如何使用javascript通過編號類對div進行排序?
- 16. 如何使用jQuery添加多個時間到一個div?
- 17. 如何在DIV中使用JavaScript中的DIV ID進行懸停效果?
- 18. 當使用Javascript添加內容時,使HTML div擴展
- 19. 只能在一個html對象div或id中使用javascript
- 20. 如何爲創建的每個div ID添加一個數字?
- 21. 當使用jquery刪除一個對象時得到div ID
- 22. 在一個div添加輸入與JavaScript
- 23. JavaScript的樣式添加到一個div只有當DIV類存在
- 24. 使用Javascript增加div中的數字
- 25. 如何使用JavaScript重新加載div?
- 26. 如何在兩個其他div之間使用javascript添加div內容塊?
- 27. 使用JavaScript添加div來
- 28. 使用jQuery對DIV中的DIV進行排序
- 29. 如何在兩個div之間根據ID使用Jquery添加Div?
- 30. 如何在javascript中添加多個div
讓我成爲第一個要注意的是純數字「ID」值無效。 – Pointy 2010-05-10 22:31:41
...除非(也許)您使用HTML5 :-)(謝謝@edwin!) – Pointy 2010-05-10 22:44:45