jQuery 中 $.remove() 的问题
简单来说...
我有元素clone
。它的 div
中保存了一些其他标签。它还包含 .x
。
我需要删除它,然后将修改后的元素应用到另一个元素。
不幸的是,它不起作用。删除失败什么的,但是 .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.
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 contains .x element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以这样做:
请注意,您可以使用
.detach()
而不是.remove()
。这将保留附加到该元素的任何元数据。You can do it like this:
Note that you can use
.detach()
instead of.remove()
. This will hold onto any metadata attached to the element.您是否试图从克隆中删除一个类?如果是,则使用removeClass,如下所示:
Are you trying to remove a class from clone? If yes, then use removeClass as follows:
这是因为 remove() 从 DOM 中删除匹配的元素。即使您传递选择器,它也仅用于过滤这些元素。在您的代码中,
clone
匹配单个元素(克隆的字幕),该元素不会公开x
类。您可以使用 find() 来匹配
.x
元素: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 thex
class.You can use find() to match the
.x
element:remove() 用于删除元素。为了删除一个类,您需要使用removeClass(className)。
remove() is for deleting elements. In order to remove a class you need to use removeClass(className).
您要从克隆内部删除 .x 元素吗?
Are you going to delete .x element from inside the clone?