gpt4 book ai didi

javascript - jQuery $ ("#myComplexId").remove() 不适用于特殊字符 vs doc.getElementById().remove() 工作得很好

转载 作者:行者123 更新时间:2023-12-01 03:36:05 24 4
gpt4 key购买 nike

所以我正在使用:

let idOfParentElement= "xd%63-3?',";
$(`#${idOfParentElement}`).remove(); //does not work

但是当我使用文档通过 id 方法获取元素并删除它时效果很好:

document.getElementById(`${idOfParentElement}`).remove();

为什么 jQuery 方法不能处理复杂的 id?

最佳答案

根据 jQuery docs of selectors :

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \. For example, an element with id="foo.bar", can use the selector $("#foo\.bar"). The W3C CSS specification contains the complete set of rules regarding valid CSS selectors. Also useful is the blog entry by Mathias Bynens on CSS character escape sequences for identifiers.

<小时/>

因此,您需要转义选择器中在 jQuery 中具有特殊含义的任何元字符。您可以使用jQuery.escapeSelector()转义这些元字符的方法(在 jQuery 版本 3 中添加)。

关于javascript - jQuery $ ("#myComplexId").remove() 不适用于特殊字符 vs doc.getElementById().remove() 工作得很好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44281884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com