Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery插件中的函数_Jquery_Function_Jquery Plugins - Fatal编程技术网

jQuery插件中的函数

jQuery插件中的函数,jquery,function,jquery-plugins,Jquery,Function,Jquery Plugins,我编写了一个插件,允许用户选择表格单元格: 我已经完成了主要功能,但是,我有一个问题。我需要能够将选择的功能作为插件选项传递。你会看到我在小提琴上的评论 问题是,该函数只应在mouseDown=1时执行,但在mousemove 感谢您的帮助:)将您的选项更改为 selected: function() { } 因此,当调用插件时,您可以执行以下操作 $('table').cellSelect({ selected:function() { alert('b');

我编写了一个插件,允许用户选择表格单元格:

我已经完成了主要功能,但是,我有一个问题。我需要能够将
选择的
功能作为插件选项传递。你会看到我在小提琴上的评论

问题是,该函数只应在
mouseDown=1
时执行,但在
mousemove

感谢您的帮助:)

将您的选项更改为

selected: function() {

}
因此,当调用插件时,您可以执行以下操作

$('table').cellSelect({
    selected:function() {
        alert('b');
    }
});
我更新了你的小提琴

相关变化:

在初始化中,您将传递匿名函数:

$(function() {
    $('table').cellSelect({ selected: function() { alert("foo"); }});
});
然后,在需要时,运行分配给
选项的功能。选择的
参数:

if (mouseDown === 1) {
    if (shiftDown === 1) {
        $(this).removeClass('selected');
    } else {
        $(this).addClass('selected');
        options.selected; // run passed anonymous function, or default specified one.
    }
}
给你:

只需使用
选项。选中的
使用初始化:

$('table').cellSelect({selected: function() { 
                                    alert('selected'); }
                              });
并呼叫您的处理程序:

// Needs to execute here
if (options.selected)
    options.selected();
}

代码:

对,那么问题出在哪里
selected
不应该用引号括起来。@RichardD问题是,函数只应该在mouseDown=1时执行,但它在mousemove上执行。您可能应该在问题中提到这一点;)但问题是,该函数只应在
mouseDown
=1时执行,但它在mousemove上执行问题是,该函数只应在mouseDown=1时执行,但它在mousemoveyes上执行,但它也在
if(mouseDown==1)
语句中问题是,该函数只应在mouseDown=1时执行,但它在mousemovit上执行,因为您已将处理程序直接添加到
mousemove
}.mousemove(options.selected)。单击(function(){
。尝试此函数:此函数将触发少量事件:。但您仍需要调试它。到达目标:)但是,如果我希望函数像这样(
$(this).text('selected')
),那么所选表格单元格的文本将发生变化。这可能吗?是的,在调用函数时只需将this作为参数传递,就可以得到触发了
onmousemove
事件的元素。