我有一个问题。现在jQuery跨度到输入,反之亦然
$(function() {
$('span').live('click', function() {
var input = $('<input />', {
'type': 'text',
'name': 'aname',
'value': $(this).html()
});
$(this).parent().append(input);
$(this).remove();
input.focus();
});
$('input').live('blur', function() {
$(this).parent().append($('<span />').html($(this).val()));
$(this).remove();
});
});
和HTML:看代码波纹管
<span>Click aici</span>
所以,这它的工作原理,很明显,直到jQuery的1.8.3(含)。在1.8.3 .live()被弃用后,我们需要使用.on()。因此,代码变成:
$(function() {
$('span').on('click', function() {
var input = $('<input />', {
'type': 'text',
'name': 'aname',
'value': $(this).html()
});
$(this).parent().append(input);
$(this).remove();
input.focus();
});
$('input').on('blur', function() {
$(this).parent().append($('<span />').html($(this).val()));
$(this).remove();
});
});
或者只是:
$(function() {
$('span').click(function() {
var input = $('<input />', {
'type': 'text',
'name': 'aname',
'value': $(this).html()
});
$(this).parent().append(input);
$(this).remove();
input.focus();
});
$('input').blur(function() {
$(this).parent().append($('<span />').html($(this).val()));
$(this).remove();
});
});
但这只是工作的第一次。
在这里看到演示:http://jsfiddle.net/hW3vk/ 所以,任何想法如何做,将不胜感激。
预先感谢您。
你所说的“第一时间”是什么意思?如果我点击和关闭文本,它可以正常工作。 – DevlshOne
工作正常。有什么问题...? –
他意味着与第二个代码,而不是.live代码,你可以看到这里http://jsfiddle.net/hW3vk/4/ – Anton