gpt4 book ai didi

javascript - 我怎样才能使一个微小的 JavaScript 表单出现在我选择的位置的对话框中?

转载 作者:行者123 更新时间:2023-11-28 00:25:39 24 4
gpt4 key购买 nike

我有一个 HTML5 Canvas 。当用户双击该 Canvas 的特定区域时,我希望在该位置出现一个单字段表单,以便用户可以编辑一些文本。

我的预期效果类似于 Google Docs 的截图。我双击带有“Hello”文本的形状,出现了可编辑的文本字段。

我已经弄清楚如何检测双击、鼠标位置和文本。但到目前为止,我使用的是 javascript“prompt(...)”对话框来编辑文本,这不是我想要的。

enter image description here

我想我正在寻找可以放入此函数中的东西来替换提示:

// let the user edit the text, by showing a popup-field that appears at (x, y)
function editText(x, y, text) {
return prompt('Text:', text);
}

如果相关,我会使用一点 jQuery。

最佳答案

爱普瑞斯似乎并没有满足您的所有要求,所以我写了这个:http://jsfiddle.net/Qj9QR/12/

CSS

.prompt { position: absolute; background-color: #E0ECFF; border-radius: 3px; width: 200px; height: 50px; border: 1px solid #99C0FF }
.prompt textarea { margin: 4px 0 0 4px }
.prompt .hint { position: absolute; bottom: 1px; left: 5px; font-size: 9px; color: #444466 }

JS 使用 jQuery see fiddle

function prompt(opts){
this.opts = $.extend({}, this.defaults, opts);
this.init();
this.open();
}

prompt.prototype = {
defaults: { x: 0, y: 0, text: 'Type Here' },
init: function(){
this.create();
this.bind();
this.position();
},
bind: function(){
var self = this;
this.el.click(false).find('textarea').bind('keypress', function(e){
if(e.which === 13) self.close(e);
});
$(document).bind('click.prompt', $.proxy(self.close, self));
},
close: function(e){
if(this.opts.close){
this.opts.close.apply(this, arguments);
}
if(!e.isPropagationStopped()){
$(document).unbind('click', this.close);
this.el.remove();
}
},
create: function(){
this.el = $('<div class="prompt" />')
.append('<textarea></textarea>')
.find('textarea').val(this.opts.text)
.end()
.append('<span class="hint">Hint: use Shift-Enter for multiple lines</span>');
},
position: function(){
this.el.css({top: this.opts.y, left: this.opts.x });
},
open: function(){
this.el.appendTo('body');
this.el.show('fast')
.find('textarea').focus().select();
}
};
// IN USE: when you click an LI update some html
$('li').click(function(){
var li = $(this),
pos = li.position();
new prompt({
text: li.html(),
x: pos.left,
y: pos.top + 17,
close: function(e){
var newText = $(e.target).val();
newText && li.html(newText);
}
});
return false;
});

Updated with position and updating element values

关于javascript - 我怎样才能使一个微小的 JavaScript 表单出现在我选择的位置的对话框中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6072612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com