2013-01-10 28 views
11

我需要一個函數運行時的div數據頁的索引屬性更改jQuery的事件

var active = $('.swipeview-active'), 
     dpi = parseInt(active.attr('data-page-index')), 
     left = $('[data-page-index="' +prev+ '"]').children("img").eq(0), 
     right = $('[data-page-index="' +next+ '"]').children("img").eq(0); 

    $(active.attr('data-page-index')).change(function(){ 

    right.clone(true). css({'z-index': 5}). fadeIn(2000).appendTo('#right'); 
    left.clone(true). css({'z-index': 5}). fadeIn(2000).appendTo('#left'); 
    }); 

我試圖改變()函數,但似乎只與輸入地裏幹活,

$(active.attr('data-page-index')).change(function(){  

是否有另一種實現此目的的方法?謝謝。

+0

我這個曾經有一天,http://meetselva.github.io/attrchange/相當不錯的插件。 – christoshrousis

+0

在這裏檢查答案 http://stackoverflow.com/questions/16781778/detecting-attribute-change-of-value-of-an-attribute-i-made –

回答

3

看來你仍然必須操縱DOM來觸發事件?如果是這樣,您可以操縱隱藏的輸入值,而不是操縱其他元素的數據屬性。然後你可以使用change觸發器。

var active = $('.swipeview-active'), 
     dpi = parseInt(active.attr('data-page-index')), 
     left = $('[data-page-index="' +prev+ '"]').children("img").eq(0), 
     right = $('[data-page-index="' +next+ '"]').children("img").eq(0), 
     dpiInput = $('.swipeview-active input:hide'); 

    dpiInput.change(function(){ 
    right.clone(true). css({'z-index': 5}). fadeIn(2000).appendTo('#right'); 
    left.clone(true). css({'z-index': 5}). fadeIn(2000).appendTo('#left'); 
    }); 

觸發:

$('.swipeview-active input:hide').val(1).trigger('change');