开发者

Problems with $.remove() in jQuery

开发者 https://www.devze.com 2023-03-30 08:40 出处:网络
To put it simple... I have element clone. Its div with some other tags saved in it. It also have .x in it.

To put it simple...

I have element clone. Its div with some other tags saved in it. It also have .x in it.

I need to remove it and then apped that modified element to another element.

Unfortunately, it doesn't work. Remove failed or something, but .x is still in it.

clone = subtitle.clone(); // Works!
no_label = clone.remove('.x'); // This fails.
more_subtitles.append(no_label); // Its appends no_label, but it still conta开发者_StackOverflowins .x element.


That's because remove() removes the matched elements from the DOM. Even if you pass a selector, it's only used to filter these elements. In your code, clone matches a single element (the cloned subtitle) which doesn't expose the x class.

You can use find() to match the .x element:

more_subtitles.append(subtitle.clone().find(".x").remove().end());


remove() is for deleting elements. In order to remove a class you need to use removeClass(className).


Are you going to delete .x element from inside the clone?

 clone.find('.x').remove()


You can do it like this:

clone = subtitle.clone();
no_label = clone.find('.x').detach();
more_subtitles.append(clone);

Note that you can use .detach() instead of .remove(). This will hold onto any metadata attached to the element.


Are you trying to remove a class from clone? If yes, then use removeClass as follows:

clone.removeClass('x');
0

精彩评论

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

关注公众号