我一直在寻找在 jQuery 中开发一个长点击插件来处理这样的事件,经过大量研究,我找到了最好的方法。我在下面创建了插件。它不是一个大文件,但它涵盖了我需要的内容。虽然它有一些问题......
$(function($) {
var holdTimer;
$.fn.longclick = function( handler, time ) {
if(time == undefined) time = 500;
return this.mouseup(function(){
clearTimeout(holdTimer);
}).mousedown(function(){
holdTimer = window.setTimeout(handler, time);
});
};
$.fn.longclick.defaultTime = 500;
}(jQuery));
下面,我有测试它的页面:
// some markup
<input type="button" name="button3" id="button3" value="1500ms">
// and... the script itself
$("#button3").longclick( function() {
var initial_text = $("#button3").val();
console.log( $(this) );
var initial_id = $(this).attr("id"); console.log( initial_id);
var initial_html = $(this).html(); console.log(initial_html);
$("#button3").replaceWith('<input type="textbox" id="" value=' + initial_text + '>');
}, 1500);
现在,我的插件的问题似乎是它不知道是什么$(this)
意思。当我返回窗口本身时,而不是我需要的按钮......另外console.log
,并且是。我怎样才能完成这项工作?$(this)
initial_id
initial_html
undefined
更新:在我的initial_html
情况下,变量应该是<input type="button" name="button3" id="button3" value="1500ms">
. jQuery.html()
不会像我预期的那样工作$(this).html()
。如何获取元素的 HTML?