问题描述
我有一个有趣的问题,我想我找到了根本原因,但是我想确定一下.我有一个链接,调用了称为remove()的函数.除Chrome之外的所有浏览器均未使用该功能.但是,即使我按以下示例简化了功能,单击的链接在Chrome中也消失了.我已经看到了以下问题:无法使用下载"作为javascript中的函数名称.但是,在链接中,我没有看到有关删除"作为保留关键字的任何信息.我的问题是,关于这个关键字,我是正确的吗?如果是这样,我在哪里可以找到Google关键字列表?我已经搜索过,但没有发现这是其他任何地方的问题.
I have an interesting problem, and I think I got to the root of it, but I wanted to be sure. I have a link that calls a function called remove(). All browsers except Chrome had no issues with the function. However, the link that is clicked disappeared in Chrome, even when I simplified the function as in the example below. I have seen this question: Can't use "download" as a function name in javascript. In the links, however, I did not see anything about "remove" as a reserved keyword. My question is this, I am correct about this being a keyword? If so, is there anywhere I can find a list of Google keywords? I have searched and have not found this to be a problem anywhere else.
<a href="javascript:void(0)" onclick="remove()">Remove</a>
JavaScript:
Javascript:
function remove(){
alert("Hi");
}
推荐答案
Chrome中的元素具有 .remove()
方法,该方法可以自动删除元素,而不必从中删除父母.
Elements in Chrome have a .remove()
method which allows for self-removal of an element instead of having to do it from the parent.
问题在于,使用属性处理程序时,您会获得不同的作用域链.该范围链包括元素本身以及 document
.这意味着元素和 document
的所有属性都显示为变量.
The trouble is that when using attribute handlers, you get a different scope chain. That scope chain includes the element itself, as well as the document
. This means that all properties of the element and document
show up as variables.
因为您将函数命名为 remove()
,并且由于它是全局函数/变量,所以它被 .remove
属性(现在是变量)遮盖了).这可以通过警报看到.如果您将处理程序更改为:
Because you named your function remove()
, and because it's a global function/variable, it is being shadowed by the .remove
property (now variable) on the element itself. This can be seen with an alert. If you change your handler to:
onclick="alert(remove)"
...您将得到类似的东西:
...you'll get something like:
function remove() { [native code] }
因此,并不是保留它,而是将其用作最终遮盖全局的属性.
So it's not that it's reserved, but rather that it's used as a property which ends up shadowing the global.
要解决此问题,请直接使用全局变量:
To fix it, either use the global directly:
onclick="window.remove()"
或更改函数名称.
这篇关于是“删除"吗?Google Chrome浏览器中的保留关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!