私はjavascriptを使ってアプリケーションを開発しています。私が必要としているのは、idが(1,2,3 ...)のdivを持ち、jqueryを使用して2と3の間にdivを挿入できるようにすることです。これを新しい3にすると、3は次のようになります。 4、4が5になります。divの挿入が機能しているので、divを並べ替える方法を知っておく必要があります。何か案は?
4 に答える
3
新しいdivを挿入した後、次のことができます。
var i = 1;
$('div').each(function() {
$(this).attr('id', i++);
});
$('div')を独自のセレクターに置き換えます。
また、使用するHTMLのバージョンによっては、IDを数字で始めることができないことにも注意してください。
于 2010-05-10T22:32:16.603 に答える
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);
于 2010-05-10T22:44:45.280 に答える
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 #
});
};
于 2010-05-10T22:53:32.823 に答える
0
jQueryセレクターからDOMの順序で物事を取り戻すことができると確信しているので、親要素を見つけて子<div>
要素を選択.each()
し、リストから取得することはできませんか?
于 2010-05-10T22:33:00.940 に答える