在连接字符串以使用Javascript组合新的DOM元素时,它只需要第一个空格分隔值

Sop*_*oph 1 javascript jquery dom

我有以下代码

var aVar = "sample text";
alert(aVar);
$(this).replaceWith( "<input type='text' value=" + aVar + " id='laID' style='width: 100px; padding-right:20px; color:white;'/>");
Run Code Online (Sandbox Code Playgroud)

警报消息框返回预期值:"示例文本".但是,DOM元素使用以下值生成:"sample".现在,如果我按照以下方式尝试它:

$(this).replaceWith( "<input type='text' value='sample text' id='laID' style='width: 100px; padding-right:20px; color:white;'/>");
Run Code Online (Sandbox Code Playgroud)

不幸的是,我别无选择,只能从变量中读取值,我不能硬编码.希望有人能指导我这个!thankss

Wri*_*ken 8

更改:

value=" + aVar + " id='
Run Code Online (Sandbox Code Playgroud)

至:

value='" + aVar + "' id='
Run Code Online (Sandbox Code Playgroud)

应引用属性.


Tom*_*omm 5

你错过了引号.用这个:

$(this).replaceWith( "<input type='text' value='" + aVar + "' id='laID' style='width: 100px; padding-right:20px; color:white;'/>");
Run Code Online (Sandbox Code Playgroud)