我试图将其设置为变量:
<svg width="500" height="400" xmlns="http://www.w3.org/2000/svg"> <!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ --> <g> <title>background</title> <rect fill="#fff" id="canvas_background" height="402" width="502" y="-1" x="-1"/> <g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid"> <rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/> </g> </g> <g> <title>Layer 1</title> <ellipse ry="69" rx="75" id="svg_1" cy="172" cx="207" fill-opacity="0.7" stroke-width="2" stroke="#995757" fill="#FF8787"/> </g> </svg>
问题是我希望将此作为存储在变量中的文本,但是起始&#34;&#34;从文本本身的&#34;&#34;中关闭。
我该如何正确设置?
e.g: var svgSource = "<svg width="500" height="400" xmlns="http://www.w3.org/2000/svg">
<!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ -->
<g>
<title>background</title>
<rect fill="#fff" id="canvas_background" height="402" width="502" y="-1" x="-1"/>
<g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid">
<rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/>
</g>
</g>
<g>
<title>Layer 1</title>
<ellipse ry="69" rx="75" id="svg_1" cy="172" cx="207" fill-opacity="0.7" stroke-width="2" stroke="#995757" fill="#FF8787"/>
</g>
</svg>"
答案 0 :(得分:2)
使用单引号。
var svgSource = '<svg width="500" height="400" xmlns="http://www.w3.org/2000/svg"> <!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ --> <g> <title>background</title> <rect fill="#fff" id="canvas_background" height="402" width="502" y="-1" x="-1"/> <g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid"> <rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/> </g> </g> <g> <title>Layer 1</title> <ellipse ry="69" rx="75" id="svg_1" cy="172" cx="207" fill-opacity="0.7" stroke-width="2" stroke="#995757" fill="#FF8787"/> </g> </svg>'
我仔细研究过 - 你的整个字符串没有一个单引号,只有两倍。所以只需使用单引号将其包装起来就可以了。
答案 1 :(得分:1)
您必须这样做:
"
替换 最外层 引用'
。这将消除逃避报价的需要。\
,使其成为多行字符串。答案 2 :(得分:0)
您需要使用反斜杠(\
)
var svgSource = "<svg width=\"500\" height=\"400\" xmlns=\"http://www.w3.org/2000/svg\">"
“隐藏”(转义)引号,但仍然将它们作为字符串的一部分。
答案 3 :(得分:0)
您可以使用单引号('
)完成此操作。
像这样:
var svgSource = "<svg width='500' height='400' xmlns='http://www.w3.org/2000/svg'> <!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ --> <g> <title>background</title> <rect fill='#fff' id='canvas_background' height='402' width='502' y='-1' x='-1'/> <g display='none' overflow='visible' y='0' x='0' height='100%' width='100%' id='canvasGrid'> <rect fill='url(#gridpattern)' stroke-width='0' y='0' x='0' height='100%' width='100%'/> </g> </g> <g> <title>Layer 1</title> <ellipse ry='69' rx='75' id='svg_1' cy='172' cx='207' fill-opacity='0.7' stroke-width='2' stroke='#995757' fill='#FF8787'/> </g> </svg>"
由于您所需的文字只包含双引号,您也可以将整个内容用单引号括起来。
var svgSource = '<svg width="500" height="400" xmlns="http://www.w3.org/2000/svg"> <!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ --> <g> <title>background</title> <rect fill="#fff" id="canvas_background" height="402" width="502" y="-1" x="-1"/> <g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid"> <rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/> </g> </g> <g> <title>Layer 1</title> <ellipse ry="69" rx="75" id="svg_1" cy="172" cx="207" fill-opacity="0.7" stroke-width="2" stroke="#995757" fill="#FF8787"/> </g> </svg>'