用于跳转/单击的jQuery / jQuery UI滑块事件。功能没有开火

时间:2012-02-18 21:54:46

标签: javascript jquery jquery-ui javascript-events

我正在创建一个jquery滑块来循环遍历div的集合,如果我按住滑块并拖动它然后松开。调用handleSlider()。如果我单击滑块中的随机点,则永远不会调用该函数。我试过使用其他两种没有运气的事件类型。是否有一个事件处理程序,用于“当用户点击/跳转”滑块而不滑动?

$( "#video_seek" ).slider({
        range: "max",
        step: 1,
        slide: function( event, ui ) {
        handleSlider();
        }
    });
$( "#video_seek" ).slider({ stop: function( event, ui ) {handleSlider();}});
$( "#video_seek" ).slider({ change: function( event, ui ) {handleSlider();}});

2 个答案:

答案 0 :(得分:2)

我相信你的问题在于语法。每次使用$时都会生成一个新的jQuery对象,所以

$( "#video_seek" ).slider({
        range: "max",
        step: 1,
        slide: function( event, ui ) {
        handleSlider();
        }
    });
//below are not necessarily referring to the original slider, but rather creating new sliders.
$( "#video_seek" ).slider({ stop: function( event, ui ) {handleSlider();}});
$( "#video_seek" ).slider({ change: function( event, ui ) {handleSlider();}});

@Andrew的语法正确。您需要的只是“更改”事件,您可以在原始声明中设置,如下所示:

$( "#video_seek" ).slider({
    change: function( event, ui ) {
         handleSlider();
    }
});

甚至是这样:

$( "#video_seek" ).slider({
    change: handleSlider
});

如果您需要在事后附加事件处理程序,请执行以下操作:

$( "#video_seek" ).bind( "slidechange", function(event, ui) {
     handleSlider();
});

甚至:

$( "#video_seek" ).bind( "slidechange", handleSlider);

(编辑)

将所有内容放在一起,避免生成后续的jQuery对象。

var videoSeek = $('#video_seek');
videoSeek.slider({
     slide: handleSlider
});
//added later on not necessary just for example of doing so
videoSeek.bind('slidechange', handleSlider);

function handleSlider(event, ui){
     //do stuff
}

答案 1 :(得分:0)

您需要在实际对象创建中设置事件,或者之后将它们绑定...

$( "#video_seek" ).slider({
    range: "max",
    step: 1,
    slide: function(event, ui) {handleSlider();}
    stop: function(event, ui) {handleSlider();}
    change: function( event, ui ) {handleSlider();}
});

或:

$( "#video_seek" ).slider({
    range: "max",
    step: 1,
});

$( "#video_seek" ).bind( "slidechange", function(event, ui) {
    handleSlider();
});