简体   繁体   English

在jQuery中附加HTML时转义JS中的字符

[英]Escaping character in JS while appending HTML in JQuery

I have a HTML Canvas in which I want to append couple of html canvas. 我有一个HTML画布,我想在其中添加几个html画布。

 <div>
        <canvas id="BackGroundImage" width="800" height="300"
    style="position: absolute; z-index: 0;"></canvas>
        <canvas id="canvas" width="800" height="300"
    style="position: absolute;  z-index: 1;"></canvas>
        </div> '

Like this. 像这样。

append(''<div>\
            <canvas id="BackGroundImage" width="800" height="300"\
        style="position: absolute; z-index: 0;"></canvas>\
            <canvas id="canvas" width="800" height="300"\
        style="position: absolute;  z-index: 1;"></canvas\>
            </div>'');

But this doesnt work the sapces after canvas give me error saying unterminated statements . 但是,这并不工作画布后sapces给我错误说unterminated statements

Use like this 这样使用

append('<div>'+
            '<canvas id="BackGroundImage" width="800" height="300"'+
        'style="position: absolute; z-index: 0;"></canvas>'+
            '<canvas id="canvas" width="800" height="300"'+
        'style="position: absolute;  z-index: 1;"></canvas\>'+
            '</div>');

try this 尝试这个

append('<div>'+
'<canvas id="BackGroundImage" width="800" height="300"'+
' style="position: absolute; z-index: 0;"></canvas>'+
'<canvas id="canvas" width="800" height="300"'
+' style="position: absolute;  z-index: 1;"></canvas\> </div>');

Try this. 尝试这个。

issue 1: append(' ' // string is not properly created. 问题1:append(''//字符串创建不正确。

issue 2 : </canvas\\> 问题2: </canvas\\>

 $("body").append('<div> I am added, inspect me in dev console and found the desired html\\ <canvas id="BackGroundImage" width="800" height="300"\\ style="position: absolute; z-index: 0;"></canvas>\\ <canvas id="canvas" width="800" height="300"\\ style="position: absolute; z-index: 1;"></canvas>\\ </div>'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

You are almost right, but the problem is you used / instead of + , some other people make problems by adding ' inside '' or " inside "" 您几乎是对的,但是问题是您使用了/而不是+ ,其他一些人通过添加' inside ''" inside ""

 $('.here').append('<div>' + '<canvas id="BackGroundImage" width="800" height="300"' + 'style="position: absolute; z-index: 0;"></canvas>' + '<canvas id="canvas" width="800" height="300"' + 'style="position: absolute; z-index: 1;"></canvas\\>' + '</div>'+ '<h1>Added</h1>'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="here"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM