[英]jQuery passing parameters to plugin
I'm working on my first jQuery plugin, and have some troubles with passing 0 as a parameter. 我正在开发我的第一个jQuery插件,并且在传递0作为参数时遇到了一些麻烦。
What I want to do is to set element.eq(parameter)
and this works just fine with 1, 2, 3, 4 and so on, but 0 won't work. 我想做的是设置element.eq(parameter)
,这对于1、2、3、4等都可以正常工作,但是0无效。 It works if I type in element.eq(0)
. 如果我输入element.eq(0)
它将起作用。
My console.log
outputs: 我的console.log
输出:
0
null x2
I have a simple jsfiddle with the problem 我有一个简单的jsfiddle问题
So how would I go about to make it show first child? 那么我将如何让它显示第一个孩子?
$.fn.miobi.defaultOptions = {
page: null,
pageActive: null
};
$('body').miobi({
page: '.page',
pageActive: 1 // not working with 0
});
The problem is that 0
is a falsey value, so rather than testing if it's truthy (it isn't), explicitly check for it being not-undefined: 问题在于0
是一个虚假值,因此与其测试是否真实(不是),不如检查它是否未定义:
if (typeof options.pageActive !== 'undefined') {
Here ya go 你去
http://jsfiddle.net/Phatjam98/gqg8qnqm/ http://jsfiddle.net/Phatjam98/gqg8qnqm/
So your if is throwing false because 0 is falsey in JS. 所以您的if会抛出false,因为JS中的false为0。 So change: 所以改变:
if(options.pageActive){
To: 至:
if(options.pageActive !== undefined){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.