2014-01-07 64 views
2

任何人都可以指出爲什麼我無法在第一次點擊時獲得警報彈出的問題嗎?它僅適用於每次點擊(即不適用於奇數點擊並適用於偶數點擊)。只有第二次點擊的jQuery工作

HTML:

<div class="slider"> 
    <div class="slider-box ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all"> 
     <a class="ui-slider-handle ui-state-default ui-corner-all" href="#" style="left: 50%;"></a> 
    </div> 
</div> 

的jQuery:

$(document).ready(function() { 
    $("a.ui-slider-handle").click(function() { 
     alert('test'); 
    }); 
}); 

我也試過,但沒有工作

$(document).ready(function() { 
    $("a.ui-slider-handle").mousedown(function() { 
     alert('test'); 
    }); 
}); 
+4

對我的作品 - > http://jsfiddle.net/3zEX5/ – adeneo

+0

是這個jQuery的ui滑塊? –

+2

可能存在干擾點擊事件的事情,如另一個點擊事件。 – bjb568

回答

2

嗯......你有沒有試圖阻止默認的錨定行爲? http://jsbin.com/IfirEBOj/2/edit

$("a.ui-slider-handle").click(function(event) { 
    event.preventDefault(); 
    alert('test'); 
}); 

無論如何,您應該熟悉一些調試工具,看看它會拋出什麼錯誤。如果有任何...

0

你試過作廢href屬性?

$(document).ready(function(){ 

    $("a.ui-slider-handle").attr('href', 'javascript:void(0)').click(function(){ 

     alert('test'); 

    }); 

}); 
0

試試這個:

<div id="slider"></div> 
$(document).ready(function() { 
    $("#slider").slider(); 
    $("#slider").on('click','a',function() { 
     alert('test'); 
    }); 
}); 

檢查小提琴: http://jsfiddle.net/3zEX5/2/

UPDATE: 我想你點擊DIV第一,然後錨標籤靠近它即小滑塊按鈕。所以你的感覺,因爲它是工作在奇數一下,如果你能明確告訴你需求量的話,可以建議你一些方法

2
add: return false; to your command 

$(document).ready(function() { 
    $("a.ui-slider-handle").click(function() { 
     alert('test'); 
return false; 
    }); 
});