JavaScript和Jquery:尋找被點擊元素的索引
P粉022140576
P粉022140576 2023-08-25 14:55:21
0
1
462

我正在建立一個投影片,我在以下問題上遇到了困難 - 當點擊標籤時,如何在陣列中找到位置(索引)?下面的程式碼部分取得了所有的a標籤

this.thumbs = this.nav.find('a');

從那裡開始呢? 還有一個問題 - 當點擊標籤時,我需要切換標籤內部div的類別(div標籤需要取得class promo_tumb_current,而具有該標籤的div需要失去它)。

HTML代碼:

JS代碼:

<腳本> 函數滑桿(容器,導航){ 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(); })

P粉022140576
P粉022140576

全部回覆 (1)
P粉426780515

我認為你需要的是

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

#

例如,在你的事件處理程序中(其中this是被點擊的a標籤):

var index = thumbs.index($(this))
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!