问题描述
||
我有一个插件,jKey,人们不断报告它无法正常工作的IE错误。该错误不是与我的脚本或jQuery有关,而是IE不支持窗口按键。我想通过检查是否选择了“ 0”来“自动更正”(我在下面的检查中完成了,如果“ 1”具有2),并且我想将1切换为4。
我没有运气。我不断得到:
Uncaught ReferenceError: Invalid left-hand side in assignment
我试过了:
if($(this)[0].parentNode == undefined){
$(this) = $(document);
}
和:
if($(this)[0].parentNode == undefined){
this = document;
}
有任何想法吗?另外,如果您有更好的方式来检查window
,请告诉我!
解决方法
$
是一个功能。 $(this)
是函数调用。您不能为函数调用分配值。
this
从不直接分配。
您是真的将jQuery修饰成this
,然后立即将其展开吗?
$(this)[0].parentNode
// is exactly equivalent to
this.parentNode
另外,如果您有更好的检查窗口的方法,请告诉我!
在插件中,别名“ 1”是您可以分配的关键字。常见的选择是self
:
// plugin entry point
var self = this;
// use self instead of this throughout the rest of the plugin
现在,您实际上可以更改\“ scope:\”
if (!this.parentNode) {
self = document;
}
您的代码段似乎会将所有选择都转换为文档?不只是窗户?因此将“ 18”转换为文档
哦,对,您正在谈论的是jQuery插件。 this
可能已经是一个jQuery对象,在这种情况下this.parentNode
始终是虚假值。您可以打开它:
if (!this[0].parentNode) {
self = $(document);
}
或使用
if (this.first().parent().length) {
self = $(document);
}