[英]Argument 1 of Window.getComputedStyle does not implement interface Element
I have this error, but I don't really know why: 我有这个错误,但我不知道为什么:
Argument 1 of Window.getComputedStyle does not implement interface Element
Window.getComputedStyle的参数1未实现接口Element
HTML: HTML:
<div class="reveal"></div>
JavaScript / jQuery: JavaScript / jQuery:
var reveal = $('.reveal');
reveal.css('margin', '10px');
var resulte = window.getComputedStyle(reveal, 'margin');
getComputedStyle()
is a JavaScript function that expects a JavaScript object, not a jQuery object. getComputedStyle()
是一个JavaScript函数,它需要一个JavaScript对象,而不是一个jQuery对象。 Pass it reveal[0]
and it will work. 传递它
reveal[0]
它会工作。
The second argument of the getComputedStyle()
function is optional and it is for the pseudo element, not the CSS property. getComputedStyle()
函数的第二个参数是可选的,它用于伪元素,而不是CSS属性。 You can use getComputedStyle()
to get all the properties and then use getPropertyValue('margin')
to get the specific property that you want. 您可以使用
getComputedStyle()
获取所有属性,然后使用getPropertyValue('margin')
来获取所需的特定属性。
The problem is when you assign a value to the margin
property in jQuery like this reveal.css('margin', '10px')
, then it gets applies to each of the margins (top, right, bottom, and left) and the margin
property will return nothing (in some browsers). 问题是当你在jQuery中为
margin
属性分配一个值时,就像这个reveal.css('margin', '10px')
,它会应用于每个边距(顶部,右边,底部和左边)和margin
属性不会返回任何内容(在某些浏览器中)。 You'll have to get each margin separately. 你必须分别获得每个保证金。
var reveal = $('.reveal'); reveal.css('margin', '10px'); var resulte = window.getComputedStyle(reveal[0], null).getPropertyValue('margin-top'); console.log(resulte);
.reveal { background-color: #f00; height: 50px; width: 50px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="reveal"></div>
The problem here is that you're passing a jQuery
object where an Element
1 is expected. 这里的问题是你传递的是一个
jQuery
对象,其中有一个Element
1 。
You're mixing up jQuery and (vanilla/plain) JavaScript, which are NOT the same. 你混淆了jQuery和(vanilla / plain)JavaScript,它们不一样。 JavaScript is a language and jQuery is a library that allows you to deal with (primarily) the DOM.
JavaScript是一种语言,jQuery是一个允许您(主要)处理DOM的库。
A jQuery object not always interchangeable with JavaScript so you need to extract the actual matched elements. jQuery对象并不总是可以与JavaScript互换,因此您需要提取实际匹配的元素。
A jQuery object (from the top of my head) is basically an advanced iterable which contains all of it's matched elements in inside. 一个jQuery对象(从头顶开始)基本上是一个高级迭代,它包含了内部所有匹配的元素。 If you only expect one match it should be in
reveal[0]
. 如果你只想要一个匹配,它应该在
reveal[0]
。
reveal[0]
should in that case be an Element
or HTMLElement
which you can then pass to the window.getComputedStyle
2 function. 在这种情况下,
reveal[0]
应该是一个Element
或HTMLElement
,然后你可以传递给window.getComputedStyle
2函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.