Javascript setTimer函数中的JQuery get元素
我有一个点击并按住动态元素的事件Javascript setTimer函数中的JQuery get元素,javascript,jquery,Javascript,Jquery,我有一个点击并按住动态元素的事件 var timeoutId = 0; $(document).on('mousedown', '.createbtn', function(){ timeoutId = setTimeout(showNewInfoCreateDlg, 1000); }).on('mouseup mouseleave', '.createbtn', function() { clearTimeout(timeoutId);
var timeoutId = 0;
$(document).on('mousedown', '.createbtn', function(){
timeoutId = setTimeout(showNewInfoCreateDlg, 1000);
}).on('mouseup mouseleave', '.createbtn', function() {
clearTimeout(timeoutId);
});
函数是“showNewInfoCreateDlg”
我需要知道被点击和保存的元素的id是什么
function showNewInfoCreateDlg(){
alert($(this).attr('id'));
}
该函数提示“未定义”
以下是解决我的问题的JSFIDLE:
将其显式绑定到函数:
var timeoutId = 0;
$(document).on('mousedown', '.createbtn', function(){
timeoutId = setTimeout(showNewInfoCreateDlg.bind(this), 1000);
}).on('mouseup mouseleave', '.createbtn', function() {
clearTimeout(timeoutId);
});
澄清一下:
bind()。这称为显式绑定。太好了。谢谢,不客气:)