使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
我试图根据旋转木马图像当时是否处于活动状态来更改div的显示属性。 我已经让它工作但它只有在我双击时才有效。 只需单击一下,即可显示与上一个活动图像对应的div,而不是当前活动图像。 请帮忙。
=====================================
代码如下
HTML
<div class="container-fluid" id="siteImgs"> <div class="row col-xs-12 col-md-7"> <div id="myCarousel" class="carousel slide" data-ride="carousel"> <!-- Carousel indicators --> <ol class="carousel-indicators"> <li data-target="#myCarousel" data-slide-to="0" id="lg" class="active"></li> <li data-target="#myCarousel" data-slide-to="1" id="ma"></li> <li data-target="#myCarousel" data-slide-to="2" id="sz"></li> <li data-target="#myCarousel" data-slide-to="3"id="ti"></li> </ol> <!-- Carousel items --> <div class="carousel-inner"> <div class="item active"> <img src="img/work/lookingGlass.png" alt="looking glass"> </div> <div class="item"> <img src="img/work/mauriceSite.png" alt="maurice site"> </div> <div class="item"> <img src="img/work/sza.png" alt="sza"> </div> <div class="item"> <img src="img/work/tina.png" alt="tina"> </div> </div> <!-- Carousel nav --> <a class="carousel-control left" href="#myCarousel" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="carousel-control right" href="#myCarousel" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a> </div> </div> <!--INFORMATION DIVS--> <div class="row col-xs-12 col-md-5 lookingGlass lg"> <h1>THE LOOKING GLASS</h1> <p>Lorem ipsum</p> </div> <div class="row col-xs-12 col-md-5 lookingGlass ma"> <h1>MAURICEDANIELS.COM</h1> <p>Lorem ipsum</p> </div> <div class="row col-xs-12 col-md-5 lookingGlass sz"> <h1>SZA</h1> <p>Lorem ipsum</p> </div> <div class="row col-xs-12 col-md-5 lookingGlass ti"> <h1>TINA D. PHOTOGRAPHY</h1> <p>Lorem ipsum</p> </div>
JS / JQUERY
$( '.ma' ).hide(); $( '.sz' ).hide(), $( '.ti' ).hide(); $( ".carousel-control" ).click(function() { if ( $( '#lg' ).hasClass( "active" ) ) { $( '.lg' ).show(), $( '.ma' ).hide(); $( '.sz' ).hide(), $( '.ti' ).hide(); } if ( $( '#ma' ).hasClass( "active" ) ) { $( '.ma' ).show(), $( '.lg' ).hide(); $( '.sz' ).hide(), $( '.ti' ).hide(); } if ( $( '#sz' ).hasClass( "active" ) ) { $( '.sz' ).show(), $( '.lg' ).hide(); $( '.ma' ).hide(), $( '.ti' ).hide(); } if ( $( '#ti' ).hasClass( "active" ) ) { $( '.ti' ).show(), $( '.lg' ).hide(); $( '.sz' ).hide(), $( '.ma' ).hide(); } });
I am trying to change the display property of a div based on if the carousel image is active at the time. I have gotten it to work but it only works when i doubleclick. With a single click it displays the div corresponding to the previous active image instead of the current one. Please HELP.
=====================================
CODE BELOW
HTML
<div class="container-fluid" id="siteImgs"> <div class="row col-xs-12 col-md-7"> <div id="myCarousel" class="carousel slide" data-ride="carousel"> <!-- Carousel indicators --> <ol class="carousel-indicators"> <li data-target="#myCarousel" data-slide-to="0" id="lg" class="active"></li> <li data-target="#myCarousel" data-slide-to="1" id="ma"></li> <li data-target="#myCarousel" data-slide-to="2" id="sz"></li> <li data-target="#myCarousel" data-slide-to="3"id="ti"></li> </ol> <!-- Carousel items --> <div class="carousel-inner"> <div class="item active"> <img src="img/work/lookingGlass.png" alt="looking glass"> </div> <div class="item"> <img src="img/work/mauriceSite.png" alt="maurice site"> </div> <div class="item"> <img src="img/work/sza.png" alt="sza"> </div> <div class="item"> <img src="img/work/tina.png" alt="tina"> </div> </div> <!-- Carousel nav --> <a class="carousel-control left" href="#myCarousel" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="carousel-control right" href="#myCarousel" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a> </div> </div> <!--INFORMATION DIVS--> <div class="row col-xs-12 col-md-5 lookingGlass lg"> <h1>THE LOOKING GLASS</h1> <p>Lorem ipsum</p> </div> <div class="row col-xs-12 col-md-5 lookingGlass ma"> <h1>MAURICEDANIELS.COM</h1> <p>Lorem ipsum</p> </div> <div class="row col-xs-12 col-md-5 lookingGlass sz"> <h1>SZA</h1> <p>Lorem ipsum</p> </div> <div class="row col-xs-12 col-md-5 lookingGlass ti"> <h1>TINA D. PHOTOGRAPHY</h1> <p>Lorem ipsum</p> </div>
JS/JQUERY
$( '.ma' ).hide(); $( '.sz' ).hide(), $( '.ti' ).hide(); $( ".carousel-control" ).click(function() { if ( $( '#lg' ).hasClass( "active" ) ) { $( '.lg' ).show(), $( '.ma' ).hide(); $( '.sz' ).hide(), $( '.ti' ).hide(); } if ( $( '#ma' ).hasClass( "active" ) ) { $( '.ma' ).show(), $( '.lg' ).hide(); $( '.sz' ).hide(), $( '.ti' ).hide(); } if ( $( '#sz' ).hasClass( "active" ) ) { $( '.sz' ).show(), $( '.lg' ).hide(); $( '.ma' ).hide(), $( '.ti' ).hide(); } if ( $( '#ti' ).hasClass( "active" ) ) { $( '.ti' ).show(), $( '.lg' ).hide(); $( '.sz' ).hide(), $( '.ma' ).hide(); } });
原文:https://stackoverflow.com/questions/27744104
满意答案
使用更干净的方法的示例代码:
// not all values present, just a sample here NSDictionary *stringsToSounds = @{@"friend":@"You're my best friend", @"hat are you doing":@"I'm talking to you", @"chips":@"chipds"}; NSDictionary *stringsToLabels = @{@"friend":@"You're my best friend", @"hat are you doing":@"I'm talking to you", @"chips":@"Ooooh. I love chips."}; for(NSString *s in stringsToSounds) { if([string rangeOfString:s] != NSNotFound) { if(stringsToLabels[s]) { [label setText:stringsToLabels[s]]; } [self playSound:stringsToSounds[s]]; break; } }
和方法playSound看起来像:
-
(void) playSound(NSString *) soundFileName { SystemSoundID SoundID; NSString *soundfile= [[NSBundle mainBundle] pathForResource:soundFileName ofType:@"mp3"]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID ); AudioServicesPlaySystemSound(SoundID); NSLog(@"sound played"); }
Sample code using a cleaner approach:
// not all values present, just a sample here NSDictionary *stringsToSounds = @{@"friend":@"You're my best friend", @"hat are you doing":@"I'm talking to you", @"chips":@"chipds"}; NSDictionary *stringsToLabels = @{@"friend":@"You're my best friend", @"hat are you doing":@"I'm talking to you", @"chips":@"Ooooh. I love chips."}; for(NSString *s in stringsToSounds) { if([string rangeOfString:s] != NSNotFound) { if(stringsToLabels[s]) { [label setText:stringsToLabels[s]]; } [self playSound:stringsToSounds[s]]; break; } }
and method playSound looks like:
-
(void) playSound(NSString *) soundFileName { SystemSoundID SoundID; NSString *soundfile= [[NSBundle mainBundle] pathForResource:soundFileName ofType:@"mp3"]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID ); AudioServicesPlaySystemSound(SoundID); NSLog(@"sound played"); }
相关问答
更多NS 是什么意思
无法忽略带有NS标志的mod_rewrite内部重定向(Unable to ignore mod_rewrite internal redirects with NS flag)
是什么导致Firefox中的NS_ERROR_STORAGE_CONSTRAINT?(What causes NS_ERROR_STORAGE_CONSTRAINT in Firefox?)
NS前缀是什么意思?(What does the NS prefix mean?)
Azure VM DNS - 指定为“NS”和“A”记录的内容(Azure VM DNS - What to specify as 'NS' and 'A' record)
如何找出NS Log的来源是什么?(How to find out what the source of an NS Log is? [closed])
什么是noir.server / start选项:ns for?(what is noir.server/start option :ns for?)
ns-3源代码(ns-3 source code)
安装时出现NS3错误(NS3 error while installing)
当使用jquery在不同的ns中存在相同的元素时,xml解析中的问题(problems in xml parsing when the same element exists in different ns, using jquery)
相关文章
更多Solr4:Tomcat7与Solr之多核配置(Multiple Cores)
用‘button’跟‘text’组合代替‘file’,选择文件后点‘submit’,‘file’的值被清空
微信公众号接口添加菜单时错误(errcode":40017 invalid button type)
请问一个jQuery的问题,谢谢
【原创】小白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》连载五(给按钮加图标)
Android按钮根据文本框设置状态
Hadoop配置多个硬盘
在列表li里两个浮动div兼容IE6问题
Linux Shell Scripting Cookbook(Linux Shell脚本攻略_英文版.pdf)
Hadoop下运行多个SecondaryNameNode的配置
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!