admin管理员组文章数量:1356231
I have the following javascript code:
function changeButtonState(targetSelector, action, iconClass) {
var $target = $(targetSelector);
var $targetSpan = $(targetSelector + ' span');
$targetSpan.removeClass('sprite-blank').addClass(iconClass);
}
How can I make it so that the $targetSpan.removeClass(..).addClass
only work if the iconClass
has a value when the function is called. I guess what I am confused about is do I check if it is defined or do I check if it has a length of 0 or more?
I have the following javascript code:
function changeButtonState(targetSelector, action, iconClass) {
var $target = $(targetSelector);
var $targetSpan = $(targetSelector + ' span');
$targetSpan.removeClass('sprite-blank').addClass(iconClass);
}
How can I make it so that the $targetSpan.removeClass(..).addClass
only work if the iconClass
has a value when the function is called. I guess what I am confused about is do I check if it is defined or do I check if it has a length of 0 or more?
- if(iconClass && iconClass.length > 0) this way you check for null undefined and length > 0 – mons droid Commented Feb 5, 2013 at 0:04
6 Answers
Reset to default 4Just use an if statement:
if (iconClass){}
Or, typeof
:
if (typeof iconClass != 'undefined') {}
if (typeof(iconClass)=='undefined') {
// nothing was passed
}
LIVE DEMO
if ( 'undefined' != typeof iconClass ) { /**/ }
Your use case you must assume that iconClass is a string. In which case I would suggest the first if condition. The second one is probably too restrictive, it usually is only used if the person calling the function does not actually pass a 3rd parameter, or passes undefined. But if the caller passes null or empty string, the first if condition will catch those conditions as well. It is the easiest one to write and it is very mon in Javascript to simply check if (variable) { }
because it will catch a lot more and is very easy to read and write.
if (iconClass) {
// Executes if iconClass is not null, not undefined, not 0, and not empty string
}
if (typeof iconClass != 'undefined') {
// WILL execute if iconClass is null, 0, empty string
// Only will not execute if iconClass is undefined!
}
Presumably, iconClass
should be a string (a class name), so you should test to see if it's a string:
if (typeof iconClass == 'string')
or you could use a regular expression to test that it's a valid class name at the same time:
if (/^[a-z][a-z0-9]*$/i.test(iconClass))
The regular expression likely needs more characters to test for (hyphen at least), I'll leave that to you. The accepted answer to What characters are valid in CSS class names? may help.
if(iconClass.length > 0){
$targetSpan.removeClass('sprite-blank').addClass(iconClass);
}
本文标签:
版权声明:本文标题:jquery - How to check if a value passed to a JavaScript function is defined or its length is >=0? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743965597a2569827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论