为什么.html()和.text()只选择第一个单词?

Rez*_*eri 2 html javascript jquery

我有以下jquery代码获取标签的第一个单词.

var val = $(this).find('label').html(); // The value must be Graphic Designer
$(this).find('label').replaceWith('<input type=text value='+val+'); // The input's value is Graphic
Run Code Online (Sandbox Code Playgroud)

这是jsfiddle

Ser*_*gio 8

你忘了html close标签字符>和它的字符串ender ',你应该添加"你的输入.应该:

'<input type=text size=40 value="'+val+'" />'
                                ^       ^
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/vgNS8/


Bra*_*tie 7

用jQuery构建输入:

$("#editInfoBtn").click(function(){
    $(".inline-update").each(function(){
        var val = $(this).find('label').html(),
                    // build the element here
                    $input = $('<input>',{attr:{'type':'text','size':40},val:val});
                // insert it here
        $(this).find('label').replaceWith($input);
    });
});
Run Code Online (Sandbox Code Playgroud)

您可能会遇到引号问题.