动态文本框列表bug jquery

时间:2011-06-18 18:21:05

标签: javascript jquery html

我有一份文字清单。只要单击此文本,它就会成为文本框。但是,如果我双击文本框,它将变得无法使用。这是我的javascript:

$('.l').live('click', function() {
$('<input type="text" id="ltb" name="'+$(this).attr("id")+'" class="'+$(this).attr("class")+'">').insertAfter($(this)).val($(this).text());
$('#ltb').focus().blur(function() {
    $('<div id="'+$(this).attr("name")+'" class="l">'+$(this).val()+'</div>').insertAfter($(this));
    $('#'+$(this).attr("class")).text($(this).val());
    $(this).remove();
});
$('#ltb').keypress(function(event)
{
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        $('<div id="'+$(this).attr("name")+'" class="l">'+$(this).val()+'</div>').insertAfter($(this));
        $('#'+$(this).attr("class")).text($(this).val());
        $(this).remove();
    }

});
$(this).remove();
//$(this).hide();
});

包含文本的div如下所示:

<div id="xxxxxxx" class="l">Text here</div>

2 个答案:

答案 0 :(得分:0)

更改z-index的{​​{1}}参数。

答案 1 :(得分:0)

修正了它。

$('.l').live('click', function() {
    if ($('#ltb').length > 0) return;
    // rest of code here

如果您将live替换为one,它也会有效。我认为问题在于创建两个具有相同id的div,因此在创建之前检查它是否存在。

来自here

  

包含多个元素的文档   使用相同的ID无效。

更多关于one(用于绑定事件一次,几天前发现了这个读取jQuery口袋引用):

http://api.jquery.com/one/

相关问题