简体   繁体   English

jQuery中$ .remove()的问题

[英]Problems with $.remove() in jQuery

To put it simple... 简单来说......

I have element clone . 我有元素clone Its div with some other tags saved in it. 它的div与其他一些标签保存在其中。 It also have .x in it. 它也有.x

I need to remove it and then apped that modified element to another element. 我需要删除它然后将修改后的元素apped到另一个元素。

Unfortunately, it doesn't work. 不幸的是,它不起作用。 Remove failed or something, but .x is still in it. 删除失败或某事,但.x仍然在其中。

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

That's because remove() removes the matched elements from the DOM. 那是因为remove()从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. 在您的代码中, clone匹配不暴露x类的单个元素(克隆的字幕)。

You can use find() to match the .x element: 您可以使用find()匹配.x元素:

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

remove() is for deleting elements. remove()用于删除元素。 In order to remove a class you need to use removeClass(className). 要删除一个类,您需要使用removeClass(className)。

你要从克隆中删除.x元素吗?

 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() . 请注意,您可以使用.detach()而不是.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: 如果是,则使用removeClass,如下所示:

clone.removeClass('x');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM