【发布时间】:2014-02-05 23:46:19
【问题描述】:
我有一个带有一些文本的p 标签,我正在尝试使其成为contenteditable,但只能在双击时使用。当我点击它时,如何防止浏览器将光标放在 p 上,并且只有在我双击时才这样做? jQuery:
p.dblclick(function(e){
p.css("cursor", "text");
})
.focusout(function(){
p.css("cursor", "default");
})
.click(function(e){
console.log('focus p');
e.stopPropagation();
$("body").focus(); // not stopping Chrome from placing cursor
});
我可以默认将contenteditable设为false,然后在dbleclick设为true,然后再执行p.focus(),但这意味着我无法将光标放在我点击的位置。另一方面,我可以在第一次单击后使其内容可编辑,然后等待 1.5 秒的 dobuleclick,如果没有发生则取消它,但如果发生了,那么内容将是可编辑的,第二次单击将触发将光标放置在正确的位置。但是,它并不是那么流畅,并且在这 1 秒半的时间内可以编辑内容。
有什么想法吗?
回答: 如果有人感兴趣,我继续实现 timer 方法,因为我认为没有其他方法......这是代码
var DELAY = 700, clicks = 0, timer = null;
p.focusout(function(){
p.css("cursor", "default");
p.prop("contenteditable", false);
})
.click(function(e){
clicks++;
p.prop("contenteditable", true);
if(clicks == 1){
timer = setTimeout(function() {
p.prop("contenteditable", false);
//alert("Single Click"); //perform single-click action
clicks = 0; //after action performed, reset counter
}, DELAY);
} else {
clearTimeout(timer); //prevent single-click action
// alert("Double Click"); //perform double-click action
clicks = 0; //after action performed, reset counter
}
});
【问题讨论】:
标签: javascript jquery contenteditable