jQuery.fn.extend 检查第一个参数是 'document' 还是 'string' (选择器)
jQuery.fn.extend check if first param is 'document' or 'string' (selector)
我正在创建这样的 jquery 插件:
$.fn.extend({
myPlugin: function() {
//Something...
}
});
如果我使用:
$(document).myPlugin();
可以
$(parent.document).myPlugin();
可以
$("#myId").myPlugin();
应该失败
$(window).myPlugin();
应该失败
$(parent).myPlugin();
应该失败
我试过了
$.fn.extend({
myPlugin: function() {
console.log(this.selector);
}
});
但是在$(document)
和$(window)
中显示为空。
如何检查第一个参数是 document
还是 parent.document
(对于弹出窗口或 iframe)?
尝试:
if (this[0] instanceof Document || this[0] instanceof Window) {
alert ('OK');
} else {
alert ('Not OK');
}
我正在创建这样的 jquery 插件:
$.fn.extend({
myPlugin: function() {
//Something...
}
});
如果我使用:
$(document).myPlugin();
可以$(parent.document).myPlugin();
可以$("#myId").myPlugin();
应该失败$(window).myPlugin();
应该失败$(parent).myPlugin();
应该失败
我试过了
$.fn.extend({
myPlugin: function() {
console.log(this.selector);
}
});
但是在$(document)
和$(window)
中显示为空。
如何检查第一个参数是 document
还是 parent.document
(对于弹出窗口或 iframe)?
尝试:
if (this[0] instanceof Document || this[0] instanceof Window) {
alert ('OK');
} else {
alert ('Not OK');
}