Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置为带有大量“的可变文本”&引用;_Javascript_Svg - Fatal编程技术网

Javascript 设置为带有大量“的可变文本”&引用;

Javascript 设置为带有大量“的可变文本”&引用;,javascript,svg,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="can

我试图将其设置为一个变量:

<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
问题是,我希望将其作为文本存储在变量中,但开始的“”从文本本身的“”关闭

我该如何正确设置它

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=”
背景
第一层
"

您需要使用反斜杠(
\

var svgSource=“”

要“隐藏”(转义)引号,但仍将其作为字符串的一部分。

您需要使用反斜杠(
\

var svgSource=“”

要“隐藏”(转义)引号,但仍将其作为字符串的一部分。

您需要使用反斜杠(
\

var svgSource=“”

要“隐藏”(转义)引号,但仍将其作为字符串的一部分。

您需要使用反斜杠(
\

var svgSource=“”
要“隐藏”(转义)引号,但仍将其作为字符串的一部分。

必须执行以下操作:

  • 首先将最外层的引号“替换为”。这样就无需转义引号
  • 在每行末尾追加一个反斜杠
    \
    ,使其成为多行字符串
  • 你必须这样做:

  • 首先将最外层的引号“替换为”。这将消除转义引号的需要
  • 在每行末尾追加一个反斜杠
    \
    ,使其成为多行字符串
  • 你必须这样做:

  • 首先将最外层的引号“替换为”。这样就无需转义引号
  • 在每行末尾追加一个反斜杠
    \
    ,使其成为多行字符串
  • 你必须这样做:

  • 首先将最外层的引号“替换为”。这将消除转义引号的需要
  • 在每行末尾追加一个反斜杠
    \
    ,使其成为多行字符串
  • 使用单引号

    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>'
    
    var svgSource='background Layer 1'
    
    我查看了它-您的整个字符串没有一个单引号,只有双引号。因此,只需使用单引号将其包装起来,就可以了。

    使用单引号

    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>'
    
    var svgSource='background Layer 1'
    
    我查看了它-您的整个字符串没有一个单引号,只有双引号。因此,只需使用单引号将其包装起来,就可以了。

    使用单引号

    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>'
    
    var svgSource='background Layer 1'
    
    我查看了它-您的整个字符串没有一个单引号,只有双引号。因此,只需使用单引号将其包装起来,就可以了。

    使用单引号

    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>'
    
    var svgSource='background Layer 1'
    

    我查看了它-您的整个字符串没有一个单引号,只有双引号。因此,只需使用单引号将其括起来就可以了。

    您可以通过使用单引号(
    )来实现这一点

    像这样:

    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=“背景层1”
    
    由于您想要的文本只包含双引号,您也可以将整个文本用单引号括起来

    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>'
    
    var svgSource='background Layer 1'
    
    您可以使用单引号(
    )来完成这一点

    像这样:

    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=“背景层1”
    
    由于您想要的文本只包含双引号,您也可以将整个文本用单引号括起来

    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>'
    
    var svgSource='background Layer 1'
    
    您可以使用单引号(
    )来完成这一点

    像这样:

    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=“背景层1”
    
    由于您想要的文本只包含双引号,您也可以将整个文本用单引号括起来

    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>'
    
    var svgSource='background Layer 1'
    
    您可以使用单引号(
    )来完成这一点

    像这样:

    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=“背景层1”
    
    由于您想要的文本只包含双引号,您也可以将整个文本用单引号括起来

    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>'
    
    var svgSource='background Layer 1'
    

    仅使用单引号或escapeJust did,非常感谢。仅使用单引号或escapeJust did,非常感谢。仅使用单引号或escapeJust did,非常感谢。仅使用单引号或escapeJust did,非常感谢。文本巨大,当然,我不必通读课文,在每一个地方都加上反斜杠。一定有更好的答案way@NicholasKyriakides:这表示您应该将标记放入JS字符串中。这是一场调试和维护的噩梦。@FelixKling这只是为了测试smething。文本太大了,当然我不必通读文本并在所有地方加反斜杠。必须有一个更好的版本way@NicholasKyriakides:这表示您应该将标记放入JS字符串中。这是一场调试和维护的噩梦。@FelixKling这只是为了测试smething。文本太大了,当然我不必通读文本并在所有地方加反斜杠。必须有一个更好的版本way@NicholasKyriakides:这表示您应该将标记放入JS字符串中。这是一场调试和维护的噩梦。@FelixKling这只是为了测试smething。文本太大了,当然我不必通读文本并在任何地方加反斜杠。必须这样做