开发者

javascript moving cloned nodes in a table

I have a table:

id1 || desc-name - 1
id2 || desc-name - 2
id3 || desc-name - 3
id4 || desc-name - 4
id5 || desc-name - 5

I would like to clone row 3 and 4 and flip them:

id1 || desc-name - 1
id2 || desc-name - 2
id4 || desc-name - 4
id3 || desc-name - 3
id5 || desc-name - 5

I have to use clone methodologies rather than jQuery.

I have figured out how to clone the rows and dele开发者_如何转开发te the current rows out of the table, however, I can't figure out how to replace the cloned rows in the right position?

Any ideas?


Do you have jQuery in your project all, even though you are using a different methodology? If so, I believe that moving the row is as easy as calling

$('#id3').insertAfter($('#id4'));

I'm not sure how to do this is plain old JS, but I figured I would post this in case jQuery was an option for you.


Here you go:

row.parentNode.insertBefore(row, row.previousElementSibling);

Note: previousElementSibling is not implemented in older versions of IE (IE8 and below).

Live demo: http://jsfiddle.net/simevidas/8AKxZ/


Just for comparison, the jQuery equivalent is this:

$(row).insertBefore($(row).prev());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜