在jquery中插入文本

时间:2014-12-11 19:36:00

标签: javascript jquery dom

这是Jquery does not remove all content as desired问题的继续。 我现在想要在输入之前插入文本。这就是我所做的。

$(document).on('click','#phoneDiv',function(){
    phone=$("#editPhone").val();
    $("#phoneDiv").contents().filter(function() {
        return (!$(this).is(".editmode"));
    }).remove();
    $('div#editPhone').prepend(document.createTextNode(phone));
});

当然这是fiddle。它只删除文本但不插入值。

1 个答案:

答案 0 :(得分:1)

这是因为您要删除phonediv内容。所以div#editPhonephonediv的孩子)在您尝试作为前缀时不存在。

$(document).on('click','#phoneDiv',function(){
    var phone=$("#editPhone").val();
    $("#phoneDiv").contents().filter(function() {
        return (!$(this).is(".editmode"));
    }).remove();
    $('div#phoneDiv').prepend(phone); //<-- div#editPhone doesn't exist anymore, you removed it above
});

见这里:http://jsfiddle.net/KyleMuir/ftngmx4j/5/