2014-12-04 23 views
0

有可能是一個非常簡單的方法來解決我的問題......組隊,探索onSlideClick - 獲得點擊HTML元素

我(簡體)HTML:

<div class="swiper-container"> 
    <div class="swiper-wrapper"> 
     <div class="swiper-slide"> 
      <a id="user-link" href="#" data-user-id="47">..</a> 
      <a id="post-link" href="#" data-post-id="8">..</a> 
     </div> 
    </div> 
</div> 

現在,我需要的是一個簡單的方法來確定哪個鏈接被點擊,所以我可以重定向到不同的頁面。不知何故,使用Swiper API中的「onSlideClick」函數是不可能的。

我一直是這樣的:

onSlideClick: function() { 
    if ($(this).attr('id') == 'user-link') { 
     var userId = $('a').data('user-id'); 
     $.redirect('user.php', { userId: userId }); 
    } 
    if ($(this).attr('id') == 'post-link') { 
     var postId = $('a').data('post-id'); 
     $.redirect('post.php', { postId: postId }); 
    } 
} 

問題:螢火蟲告訴我,

onSlideClick: function() { 
    console.log($(this).attr("id")); 
} 

是不確定的。

我必須通過JavaScript重定向,否則我的滑動將無法正常工作,因爲我會立即重定向。

回答

0

你可以嘗試從事件目標獲得點擊的元素:

onSlideClick: function(swiper, e) { 
    var clicked = $(e.target); 
    var id = clicked.attr('id'); 
    if (id == 'user-link') { 
    var userId = clicked.data('user-id'); 
    $.redirect('user.php', { userId: userId }); 
    } 
    if (id == 'post-link') { 
    var postId = clicked.data('post-id'); 
    $.redirect('post.php', { postId: postId }); 
    } 
} 
+0

嘿,感謝您的回答,但多數民衆贊成在不工作。我也從Firebug控制檯得到「undefined」。 – Marley 2014-12-05 10:11:04

+0

@Marley你有這個例子的實時鏈接,所以我可以看看更接近? – 2014-12-05 18:41:28