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?

Share Improve this question edited Jul 25, 2019 at 13:13 Wyck 11.8k8 gold badges48 silver badges81 bronze badges asked Feb 4, 2013 at 23:59 EpikEpik 3,3574 gold badges18 silver badges23 bronze badges 1
  • if(iconClass && iconClass.length > 0) this way you check for null undefined and length > 0 – mons droid Commented Feb 5, 2013 at 0:04
Add a ment  | 

6 Answers 6

Reset to default 4

Just 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);
}

本文标签: