也许这将是simplier如果您要使用现有的解决方案。例如:jeditable
但是如果你想(见DEMO):
$(document).ready(function() {
var beginEdit = function() {
var text = $(this).text();
var input = $('<input value="' + text + '" type="text">').one('blur', endEdit);
$(this).empty().append(input);
};
var endEdit = function() {
var text = $(this).val();
$(this).parent().html(text).one('click', beginEdit);
};
$(".text").one('click', beginEdit);
});
至于jQuery的插件(见DEMO2):
$.fn.editable = function() {
this.each(function() {
var beginEdit = function() {
var text = $(this).text();
var input = $('<input value="' + text + '" type="text">').one('blur', endEdit);
$(this).empty().append(input);
};
var endEdit = function() {
var text = $(this).val();
$(this).parent().html(text).one('click', beginEdit);
};
$(this).one('click', beginEdit);
});
};
$(document).ready(function() {
$(".text").editable();
});
你的意思是...文本框的默认行为? – Brad 2012-07-31 15:36:38
是的,我需要它持有的价值。 – 2012-07-31 15:38:17
我不明白。一个文本框自己完成。如果它有一个值,你点击它,没有改变,直到你输入。 – Brad 2012-07-31 15:38:42