开发者

Why won't jQuery accept this string?

When I run this function:

onUnCheck: function(el) {
var thenames = "icon-"+el.find("label:first").text().replace(/ /g,'').toLowerCase();        
alert(thenames);            
$("'."+thenames+"'").hide("fast");
}

I generate a string. I can see in the alert() that it is indeed the correct string. Example:

icon-ji开发者_如何学编程ra

But when I pass the string in as a jQuery selector it does not work.

I know the function's logic is sound because pasting in the result of my alert() makes it work.

Why won't jQuery accept my string?


$('.'+thenames).hide("fast");

This will look for ".icon-jira." You were looking for "'.icon-jira'" which adds the quotes to the selector.


It looks like you have an extra set of single quotes (''). Try this:

onUnCheck: function(el) {
  var thenames = "icon-"+el.find("label:first").text().replace(/\s/g,'').toLowerCase();        
  alert(thenames);            
  $("."+thenames).hide("fast");
}

Also, consider using /\s/g instead of / /g -- /\s/g is a little less error prone and a bit more readable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜