检查窗口,如果是,则在jQuery Plugin中更改为文档

问题描述

|| 我有一个插件,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);
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...