I want to clone and remove ul last-child but following code is not working. Someone tell me how it will be work?
$(document).ready(function () {
tmp1 = '';
tmp = '<ul><li><h4>Menu 5</h4></li></ul><ul><li><h4>Menu 6</h4></li></ul><ul><li><h4>Menu 7</h4></li></ul>';
$(tmp).children('ul:last-child').clone().appendTo(tmp1)
$(tmp).children('ul:last').remove();
$('div').append(tmp);
});
I think you are doing it wrong: tmp
is a string.
You have to do this: http://jsfiddle.net/Lu6jA/
$(document).ready(function () {
tmp1 = '';
tmp = '<ul><li><h4>Menu 5</h4></li></ul><ul><li><h4>Menu 6</h4></li></ul><ul><li><h4>Menu 7</h4></li></ul>';
var lastUl = tmp.lastIndexOf("<ul>");
var yourclone = tmp.substring(lastUl);
var newtmp = tmp.replace(yourclone,"");
$('div').append(newtmp);
alert(yourclone); // This is for your reference
});
Explantion: First find the last UL
from the string. yourclone
contains your last ul
as a string. Now, replace last ul
from the tmp
string with nothing, so basically means remove it and place the result in newtmp
. the append it to div
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.