简体   繁体   English

Window.getComputedStyle的参数1未实现接口Element

[英]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]应该是一个ElementHTMLElement ,然后你可以传递给window.getComputedStyle 2函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Window.getComputedStyle在Firefox中未实现界面元素错误 - Window.getComputedStyle does not implement interface Element error in Firefox element.style或window.getComputedStyle()不显示float - element.style or window.getComputedStyle() does not showing float Window.getComputedStyle不显示内联样式 - Window.getComputedStyle does not show inline style 为什么 window.getComputedStyle(element).getPropertyValue("order") 不返回 flexbox 元素的顺序? - Why does window.getComputedStyle(element).getPropertyValue("order") doesn't returns order of a flexbox element? Javascript - window.getComputedStyle 返回“auto”作为元素的顶部和左侧属性 - Javascript - window.getComputedStyle returns “auto” as element top and left properties window.getComputedStyle 总是以像素为单位返回测量值 - window.getComputedStyle always returns measurement in pixels 使用 window.getComputedStyle 错误的 css 信息 - Wrong css informations by using window.getComputedStyle 如何修复Window.getComputedStyle不是对象错误 - How to fix Window.getComputedStyle is not an object error window.getComputedStyle(elem) 用于元素未悬停的情况,即使它当前已悬停 - window.getComputedStyle(elem) for the case where the element is not hovered, even though it is currently hovered 无法使用 window.getComputedStyle 或 element.offsetHeight 强制 DOM 重绘? - Cannot force DOM redraw using window.getComputedStyle or element.offsetHeight?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM