小编Leo*_*atu的帖子

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();
    });
    });
Run Code Online (Sandbox Code Playgroud)

和HTML现在:

<span>Click aici</span>
Run Code Online (Sandbox Code Playgroud)

所以,这显然适用于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();
    });
    });
Run Code Online (Sandbox Code Playgroud)

要不就:

$(function () {
    $('span').click(function () { …
Run Code Online (Sandbox Code Playgroud)

html jquery input live

0
推荐指数
1
解决办法
4756
查看次数

标签 统计

html ×1

input ×1

jquery ×1

live ×1