jQuery 错误? ("this" 成员消失)
jQuery bug? ("this" members disappear)
https://jsfiddle.net/a1eds7m0/1/
<form id="form" onkeypress="return event.keyCode != 13;">
<p><input type="text" id="arWalletKeyFile" name="arWalletKeyFile"/></p>
</form>
(function( $ ) {
$.fn.arKeyChooser = function(options) {
this.options = options || {};
}
$.fn.arKeyGet = function() {
if(!this.options) alert("jQuery bug?")
}
}( jQuery ));
$(function() {
$('#arWalletKeyFile').arKeyChooser({storeName: 'authorARPrivateKey'});
$('#arWalletKeyFile').arKeyGet()
});
this.options
神秘消失。这是 jQuery 错误还是什么?什么是解决方法?
您正在尝试为 $()
函数返回的 jQuery 包装器对象赋值。尝试使用 data()
method 代替(这会将它与您的 input [type=file]
HTML 元素相关联):
$.fn.arKeyChooser = function(options) {
// set options
this.data('options', options || {});
}
$.fn.arKeyGet = function() {
// get options
const options = this.data('options');
if(options) {
alert("jQuery bug?");
}
}
此外,我建议在每个 jQuery 插件中添加 return this;
,这样它仍然可以链接。
https://jsfiddle.net/a1eds7m0/1/
<form id="form" onkeypress="return event.keyCode != 13;">
<p><input type="text" id="arWalletKeyFile" name="arWalletKeyFile"/></p>
</form>
(function( $ ) {
$.fn.arKeyChooser = function(options) {
this.options = options || {};
}
$.fn.arKeyGet = function() {
if(!this.options) alert("jQuery bug?")
}
}( jQuery ));
$(function() {
$('#arWalletKeyFile').arKeyChooser({storeName: 'authorARPrivateKey'});
$('#arWalletKeyFile').arKeyGet()
});
this.options
神秘消失。这是 jQuery 错误还是什么?什么是解决方法?
您正在尝试为 $()
函数返回的 jQuery 包装器对象赋值。尝试使用 data()
method 代替(这会将它与您的 input [type=file]
HTML 元素相关联):
$.fn.arKeyChooser = function(options) {
// set options
this.data('options', options || {});
}
$.fn.arKeyGet = function() {
// get options
const options = this.data('options');
if(options) {
alert("jQuery bug?");
}
}
此外,我建议在每个 jQuery 插件中添加 return this;
,这样它仍然可以链接。