JavaScript和Jquery:寻找被点击元素的索引
P粉022140576
P粉022140576 2023-08-25 14:55:21
0
1
381
<p>我正在构建一个幻灯片,我在以下问题上遇到了困难 - 当点击标签时,如何在数组中找到位置(索引)?下面的代码部分获取了所有的a标签</p> <pre class="brush:php;toolbar:false;">this.thumbs = this.nav.find('a');</pre> <p>从那里开始呢? 还有一个问题 - 当点击标签时,我需要切换标签内部div的类(div标签需要获取class promo_tumb_current,而具有该标签的div需要失去它)。</p> <p>HTML代码:</p> <pre class="brush:php;toolbar:false;"><div class="promo_tumbs col_12"> <div data-dir="prev" class="prev"></div> <div data-dir="next" class="next"></div> <div class="promo_tumbs_centar"> <a href="#first"><div class="promo_tumb promo_tumb_current"></div></a> <a href="#second"><div class="promo_tumb"></div></a> <a href="#thrid"><div class="promo_tumb"></div></a> <a href="#fourh"><div class="promo_tumb"></div></a> <a href="#fifth"><div class="promo_tumb"></div></a> <a href="#fifth"><div class="promo_tumb"></div></a> <a href="#fifth"><div class="promo_tumb"></div></a> </div> </div></前> <p>JS代码:</p> <pre class="brush:php;toolbar:false;"><脚本> 函数滑块(容器,导航){ this.container = 容器; this.nav = 导航; this.li = this.container.find('li'); this.li_width = this.li.first().width(); this.li_len = this.li.length; this.thumbs = this.nav.find('a'); this.当前 = 0; } Slider.prototype.transition = 函数(坐标){ this.container.stop().animate({ 'margin-left' : 坐标 || -(this.current * this.li_width) }) } Slider.prototype.set_current = 函数(dir){ var pos = this.current; if (dir === '下一个') {pos++} else if (dir === '上一个') {pos--} this.current = (pos < 0) ? this.li_len - 1 : pos % this.li_len; 返回位置; } var slider = new Slider($('div.promo_inner ul'), $('div.promo_tumbs')); slider.nav.find('div').on('点击', function(){ if ($(this).attr("data-dir") === 未定义 ) { var index = slider.thumbs.index(); 控制台.log(索引) } 别的 { slider.set_current($(this).data('dir')); } 滑块.transition(); })</pre> <p></p>
P粉022140576
P粉022140576

全部回复(1)
P粉426780515

我认为你需要的是

http://api.jquery.com/index/

例如,在你的事件处理程序中(其中this是被点击的a标签):

var index = thumbs.index($(this))
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!