jQuery switch-case 不起作用,语法问题?
为什么这个开关盒不工作?我已经尝试了所有可能的语法,但它不起作用。或者在这种情况下 switch-case 是错误的方法吗?
jQuery('#main input').blur(function() {
switch(jQuery(this).attr('name') {
case 'email':
jQuery(this).val('That's you e-Mail adress.');
break;
case 'nickname':
jQuery(this).val('That's your nickname.');
break;
}
});
请注意:我在非冲突模式下使用 jQuery,这就是我使用 jQuery
而不是 $
的原因。
Why is this switch-case not working? I've tried every possible syntax, but it is just not working. Or is a switch-case the wrong method in this case?
jQuery('#main input').blur(function() {
switch(jQuery(this).attr('name') {
case 'email':
jQuery(this).val('That's you e-Mail adress.');
break;
case 'nickname':
jQuery(this).val('That's your nickname.');
break;
}
});
Please note: I am using jQuery in non conflict mode, that's why I use jQuery
instead of $
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有几个错误:
缺少右括号。应该是:
您应该转义该字符串中的引号:
像这样:
You have several errors in here:
Missing a closing parenthesis. Should be:
You should escape the quote in this string:
Like this:
在同一行中使用许多
'
似乎会扰乱您的语法。更改如下,Using many
'
in the same line seems to mess your syntax. change as below,您有两种类型的错误:
switch
参数周围的括号)
。'
)。更正的代码:
更新:
如果您不熟悉 JavaScript 中的转义字符串(例如上面的单词
That's
),看看这个 Stack Overflow 问题:Escaping Strings in JavaScriptYou have two types of errors:
)
closing around theswitch
parameter.'
) in the word "that's".Corrected Code:
Update:
If you are unfamiliar with escaping strings in JavaScript (such as with the word
That's
above), take a look at this Stack Overflow question: Escaping Strings in JavaScript