基于多个类之一的 switch case 语句
我正在尝试实现这样的 switch case 语句:
switch (active.attr('class')){
case "video": ...
case "slideshow": ...
...
问题是,“视频”和“幻灯片”都是我尝试定位的 div 所具有的三个类之一,因此它与大小写不匹配。 ..
我基本上会怎么说“视频是 attr('class') 的一部分”?
谢谢, 卢茨
I'm trying to implement a switch case statement like this:
switch (active.attr('class')){
case "video": ...
case "slideshow": ...
...
The problem is, that "video" and "slideshow" each are one of three classes the div I'm trying to target has, therefor it doesn't match the case...
How would I basically say "video is a part of attr('class')" ?
Thanks,
Lutz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您正在使用
jQuery
,它提供了两种方法:.is()
帮助.hasClass( )
help所以你可以调用:
或
注意
.is()
需要一个更具体的模式,以.< /code> 但你也可以确定很多其他的东西,比如
.is('div')
等。It looks like you're using
jQuery
, which offers two methods for that:.is()
help.hasClass()
helpSo you can either call:
or
Notice that
.is()
requires a more specific pattern with the leading.
but it you can determine lots of other things too like.is('div')
etc.您也许可以尝试拆分字符串。
我的脑海里浮现出:
只是一个想法。上面的答案可能会以其他方式执行相同的操作:)
编辑:
您还可以像这样使用 jQuery
.each()
:You could perhaps try and split the string.
From the top of my head:
Just an idea. The above answers might do the same in other ways :)
Edit:
You could also use jQuery
.each()
like this:您可以使用
hasClass
查看此相关帖子。 我认为确实如此你想做什么。
You can use
hasClass
See this related post. I think it does what you're looking to do.
使用 if 和 hasClass 方法:
如果您需要同时执行这两个操作(在开关上失败),删除 else 以便 if 语句是独立的。
Use if and the hasClass method:
If you need to do both (fall-through on the switch), remove the else so the if statements are independent.