admin管理员组文章数量:1394135
I want to clone the content of the div using jQuery, but from the copied content I want to remove the class from the original one before I use appendTo function. When I remove the class from a clone they also remove from the original one.
My code is:
$('.carousel .item').each(function(){
var next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
next.children(':first-child').addClass('col-sm-offset-1');
for (var i=0;i<3;i++) {
next=next.next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
}
});
Please note, I don't want to remove class from actual div from where I copied the content, I just want to remove it from the copied code so that it is not included in the cloned div.
I want to clone the content of the div using jQuery, but from the copied content I want to remove the class from the original one before I use appendTo function. When I remove the class from a clone they also remove from the original one.
My code is:
$('.carousel .item').each(function(){
var next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
next.children(':first-child').addClass('col-sm-offset-1');
for (var i=0;i<3;i++) {
next=next.next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
}
});
Please note, I don't want to remove class from actual div from where I copied the content, I just want to remove it from the copied code so that it is not included in the cloned div.
Share Improve this question edited May 25, 2017 at 7:24 Prashant Shirke 1,4231 gold badge8 silver badges10 bronze badges asked May 25, 2017 at 6:15 surajsuraj 794 bronze badges 2- i don't see code to remove class , can you update your code with remove class , where are you trying to remove class – LazyDeveloper Commented May 25, 2017 at 6:19
- next.children(':first-child').removeClass('col-sm-offset-1').clone().appendTo($(this)); //here i remove the clone div class – suraj Commented May 25, 2017 at 6:54
3 Answers
Reset to default 5You can use removeClass
and addClass
after clone
like this.
.clone().removeClass('oldClass').addClass('col-sm-offset-1')
You can remove element from cloned object first and then clone to your new object which would be something like shown below:
$('.carousel .item').each(function(){ var next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
var $block = next.children(':first-child');
// Grab the and remove element class
$block = $block.find('.your-element-identity-class').removeClass('your-element-identity-class');
// Clone the block
var $clone = $block.clone();
$clone.appendTo($(this));
next.children(':first-child').addClass('col-sm-offset-1');
for (var i=0;i<3;i++) {
next=next.next();
if (!next.length) {
next = $(this).siblings(':first');
}
var $block = next.children(':first-child');
// Grab the and remove element class
$block = $block.find('.your-element-identity-class').removeClass('your-element-identity-class');
// Clone the block
var $clone = $block.clone();
$clone.appendTo($(this));
}
});
Replace "your-element-identity-class" with your class name you want to remove. Original ref. from - How to remove the selected element during a clone() operation
You should be able to run removeClass() on the object before you append it, regardless of where you want to append it to.
本文标签: javascriptClone div and remove class without changing the original oneStack Overflow
版权声明:本文标题:javascript - Clone div and remove class without changing the original one - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744617749a2615834.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论