javascript - 设置为带有大量 ""的变量文本

标签 javascript svg

我试图将其设置为变量:

<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>

问题是我想将其作为文本存储在变量中,但起始的“”从文本本身的“”中关闭。

我该如何正确设置?

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>"

最佳答案

使用单引号。

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>'

我仔细查看了它 - 你的整个字符串没有一个单引号,只有双引号。因此只需使用单引号将其括起来即可正常工作。

关于javascript - 设置为带有大量 ""的变量文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24099403/

相关文章:

java - 请求 : https://www. w3.org/2000/svg 找到 : http://www. w3.org/2000/svg

javascript - svg :g is added时如何在div中添加滚动条

reactjs - 导入svg文件时 typescript 找不到模块

google-chrome - 动画标签中的 SVG begin ="id.end"属性不起作用

javascript - 正则表达式匹配额外不需要的内容

javascript - 无法使用 Chrome 扩展中的 postMessage 将数据从父窗口发送到 iframe

javascript - 无法将菜单按钮向右移动

javascript - 如何使用 Bootstrap 创建响应式轮播嵌入式卡片?

javascript - DOM 更新后跟一个大循环没有及时呈现

javascript - 将 d3 SVG 元素 float 到右上角 CSS