【发布时间】:2013-07-13 20:45:28
【问题描述】:
我 dblclick 一个段落并调用一个函数以用文本区域动态替换它,以便我可以编辑段落文本。我捕获段落的宽度和高度并传递文本和尺寸。
$('article').on('dblclick','p', function(ev) {
var text = $(this).text(); // save the text
var dim = $(this).css( ["width", "height"]);
$(this) // for the p element...
.empty() // wipe the p and it's text
.append(newTAInput(this, text, dim)); // append edited text
});
此函数创建文本区域,设置文本和尺寸,并在您在文本区域外单击时返回更改后的文本。
function newTAInput(target, text, dim) {
var input = $('<textarea ></textarea>')
.val(text)
.attr(dim)
.css('font-family', 'Times New Roman')
.css('font-size', '16.37px')
.bind('blur', function() {
var value = $(this).val();
$(target).html(value);
});
return input;
}
这并没有发生,即使 dim 是一个普通对象,在 .attr() 函数中具有正确的键和值。
查看 jsfiddle 中的完整代码:http://jsfiddle.net/tenrab/dYR4M/
【问题讨论】:
-
你设置的是行和列,而不是高度和宽度?
标签: javascript jquery textarea