CSS JavaScript 设置指针事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16492401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:50:09  来源:igfitidea点击:

JavaScript setting pointer-events

javascriptcss

提问by user1334130

I am trying to set pointer-events: nonefor a div through JavaScript, however the property is not being applied.

我正在尝试pointer-events: none通过 JavaScript设置一个 div,但是该属性没有被应用。

I have tried:

我试过了:

document.getElementById("div").setAttribute("pointer-events", "none");

And:

和:

document.getElementById("div").style.pointer-events = "none";

But neither worked, any ideas?

但都没有奏效,有什么想法吗?

回答by Benjamin Gruenbaum

Your first attempt failed because it was a CSS property and not an attribute (part of the styleattribute).

您的第一次尝试失败了,因为它是 CSS 属性而不是属性(属性的一部分style)。

Your second attempt failed because for some off reason, when making this API casing-like-thisgets converted to camelCasingLikeThis. This is likelybecause the guys who built JavaScript and the guys who built CSS weren't very well coordinated.

您的第二次尝试失败了,因为出于某种原因,在将此 APIcasing-like-this转换为camelCasingLikeThis. 这可能是因为构建 JavaScript 的人和构建 CSS 的人没有很好地协调。

The following should work:

以下应该工作:

document.getElementById("div").style.pointerEvents = "none";

回答by aleha

You can access style properties via Bracket notationlike this:

您可以通过Bracket 符号访问样式属性,如下所示:

document.getElementById("div").style['pointer-events'] = 'auto';

No need in camel case modification in this case.

在这种情况下不需要在驼峰式修改。

回答by Behnood

There is an examplein a bookwhich uses element.setAttributeNS(null, "pointer-events", "none");instead of using element.style.pointer-events = "none";.

本书中有一个例子,它使用而不是使用.element.setAttributeNS(null, "pointer-events", "none");element.style.pointer-events = "none";