Javascript 如何将变量与'';在jQuery中
您好,我正在尝试使用jQuery使用函数onchange with Ajax创建一个输入。这是输入:Javascript 如何将变量与'';在jQuery中,javascript,jquery,Javascript,Jquery,您好,我正在尝试使用jQuery使用函数onchange with Ajax创建一个输入。这是输入: <input type="file" class="hidden-print btn-sm" id="btn1" onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])"> 但我得到了这个控制台错误: 无法将属性“src”
<input type="file" class="hidden-print btn-sm" id="btn1"
onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])">
但我得到了这个控制台错误:
无法将属性“src”设置为null。我知道这是因为('blah')问题,因为HTML代码工作正常
当变量值附加到字符串中时,需要在变量值周围加引号。为此,您需要对字符串中的引号进行转义,以便它们不会被解释为分隔字符串,为此,您可以使用
\'
。试试这个:
getElementById(' + "blah" + ') rest of the code...
html+='';
就这样。谢谢兄弟!!帮个忙,使用DOM(或jQuery的等效)方法。我不知道DOM,但谢谢!!!我还有一件事要学。
var blah = 'blah';
html += '<input type="file" class="hidden-print btn-sm" id="btn1" onchange="document.getElementById('+ blah +').src = window.URL.createObjectURL(this.files[0])">';
getElementById(' + "blah" + ') rest of the code...
html += '<input type="file" class="hidden-print btn-sm" id="btn1" onchange="document.getElementById(\''+ blah + '\').src = window.URL.createObjectURL(this.files[0])">';