简而言之:我试图理解这个 TypeError 的含义: 无法在“Window”上执行“getComputedStyle”:参数 1 不是“Element”类型 在使用 Mediawiki 的可视化编辑器时出现错误,如下所示:
http://www.wiki.org.il/index.php?title=new-page&veaction=edit
错误不允许创建新页面或匿名编辑 wiki。 然而,使用不同的皮肤错误消失:
http://www.wiki.org.il/index.php/Main_Page?useskin=vector
维基在 1.25alpha 上运行。
我有同样的错误显示。当我用普通的 JavaScript 替换 jQuery 选择器时,错误被修复了。
var this_id = $(this).attr('id');
替换:
getComputedStyle( $('#'+this_id)[0], "")
搭配:
getComputedStyle( document.getElementById(this_id), "")
报错信息说
getComputedStyle
要求参数为Element
类型。你收到它是因为参数的类型不正确。
最常见的情况是你试图传递一个不存在的元素作为参数:
my_element = document.querySelector(#non_existing_id);
现在那个元素是
null
,这将导致提到的错误:
my_style = window.getComputedStyle(my_element);
如果不可能始终正确获取元素,例如,如果
querySelector
没有找到任何匹配项,您可以使用以下结束函数:
if (my_element === null) return;
我通过尝试
ng-include
一个不存在的 type="text/ng-template"
在 AngularJS v1.5.8 中得到它。
<div ng-include="tab.content">...</div>
确保当您使用 ng-include 时,该指令的数据指向实际的页面/部分。否则,您可能想要:
<div>{{tab.content}}</div>
就我而言,我使用的是
ClassName
.
getComputedStyle( document.getElementsByClassName(this_id)) //error
它也可以在没有第二个参数的情况下工作
" "
.
这是我完整的运行代码:
function changeFontSize(target) {
var minmax = document.getElementById("minmax");
var computedStyle = window.getComputedStyle
? getComputedStyle(minmax) // Standards
: minmax.currentStyle; // Old IE
var fontSize;
if (computedStyle) { // This will be true on nearly all browsers
fontSize = parseFloat(computedStyle && computedStyle.fontSize);
if (target == "sizePlus") {
if(fontSize<20){
fontSize += 5;
}
} else if (target == "sizeMinus") {
if(fontSize>15){
fontSize -= 5;
}
}
minmax.style.fontSize = fontSize + "px";
}
}
onclick= "changeFontSize(this.id)"
我在为自己使用而修改的 jQuery 插件中遇到了同样的问题。基本上,你可以从documentation看到,传递给
getComputedStyle
的参数必须是Element
类型。您的插件正在传递 window
,指向浏览器窗口的全局变量,它是 Window
类型(谁会猜到?)到函数调用中。他们的代码看起来像这样,我猜:
var x = window.getComputedStyle(e);
他们可能是从一个通用函数调用它的,该函数没有验证
e
实际上是一个Element
而不是一个Window
。
在我的例子中,我只是想绕过元素上的 jQuery
width()
方法并包括边框宽度和填充宽度。 (弄清楚为什么我必须这样做真是太麻烦了。)我回顾了 jQuery 如何确定元素是否为 Window
并以类似的方式处理它。最终,这就是我在用例中所做的:
function getWidth(e) {
if (e != null && e === e.window) {
return $(e).width();
}
const css = window.getComputedStyle(e[0]);
const width = parseInt(css.width);
const pLeft = parseInt(css.paddingLeft);
const pRight = parseInt(css.paddingRight);
const bLeft = parseInt(css.borderLeft);
const bRight = parseInt(css.borderRight);
return width + pLeft + pRight + bLeft + bRight;
}
我在 Angular6 项目中遇到了同样的错误。这些解决方案似乎都不适合我。原来问题是由于指定为
dropdown
但它没有下拉选项的元素引起的。看看下面的代码:
<span class="nav-link" id="navbarDropdownMenuLink" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<i class="material-icons "
style="font-size: 2rem">notifications</i>
<span class="notification"></span>
<p>
<span class="d-lg-none d-md-block">Some Actions</span>
</p>
</span>
<div class="dropdown-menu dropdown-menu-left"
*ngIf="global.localStorageItem('isInSadHich')"
aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">You have 5 new tasks</a>
<a class="dropdown-item" href="#">You're now friend with Andrew</a>
<a class="dropdown-item" href="#">Another Notification</a>
<a class="dropdown-item" href="#">Another One</a>
</div>
删除代码
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
解决了问题。
我自己认为,每次单击第一个 span 元素时,范围都希望为父 span 中不存在的下拉子项设置样式,因此会引发错误。
const el = document.getElementsByClassName('content-right')[0]
if (!el) return
const i = window.getComputedStyle(el).width
这是一种修复错误的方法,但我不明白为什么找不到元素,它不应该为空!
这不是正确的语法:
const getprop = window.getComputedStyle(element).getPropertyValue("margin-top");
这是正确的:
你只需要写一个完整的'document.querySelector(el)'
常量 getprop = window.getComputedStyle(document.querySelector('.burger__menu')).getPropertyValue("margin-top");
这将是 100% 的工作
如果您在使用输入类型为“数字”的输入字段时遇到此错误。
确保在输入更改之前您的字段已聚焦。您可以使用 javascript 函数执行此操作,以聚焦具有 id 的字段。
例如:
const inputField = document.getElementById('myInputField');
inputField.addEventListener('mouseenter', function() {
this.focus();
});
错误消息非常简单:getComputedStyle 期望一个 Element 作为它的第一个参数,并且传递给它的是其他东西。
如果你真正需要的是帮助调试你的皮肤,你应该更加努力地隔离错误。