Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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变量中存储HTML或XML代码_Javascript_Jquery_Xml_Cdata_Mechanicalturk - Fatal编程技术网

在javascript变量中存储HTML或XML代码

在javascript变量中存储HTML或XML代码,javascript,jquery,xml,cdata,mechanicalturk,Javascript,Jquery,Xml,Cdata,Mechanicalturk,我想在javascript变量中存储一些HTML/XML标记。问题是文本相对较大。例如,如何将以下XML片段存储在javascript变量中 <QuestionForm xmlns="[the QuestionForm schema URL]"> <Overview> <Title>Game 01523, "X" to play</Title> <Text> You are helping to

我想在javascript变量中存储一些HTML/XML标记。问题是文本相对较大。例如,如何将以下XML片段存储在javascript变量中

    <QuestionForm xmlns="[the QuestionForm schema URL]">
  <Overview>
    <Title>Game 01523, "X" to play</Title>
    <Text>
      You are helping to decide the next move in a game of Tic-Tac-Toe.  The board looks like this:
    </Text>
    <Binary>
      <MimeType>
        <Type>image</Type>
        <SubType>gif</SubType>
      </MimeType>
      <DataURL>http://tictactoe.amazon.com/game/01523/board.gif</DataURL>
      <AltText>The game board, with "X" to move.</AltText>
    </Binary>
    <Text>
      Player "X" has the next move.
    </Text>
  </Overview>
  <Question>
    <QuestionIdentifier>nextmove</QuestionIdentifier>
    <DisplayName>The Next Move</DisplayName>
    <IsRequired>true</IsRequired>
    <QuestionContent>
      <Text>
        What are the coordinates of the best move for player "X" in this game?
      </Text>
    </QuestionContent>
    <AnswerSpecification>
      <FreeTextAnswer>
        <Constraints>
          <Length minLength="2" maxLength="2" />
        </Constraints>
        <DefaultText>C1</DefaultText>
      </FreeTextAnswer>
    </AnswerSpecification>
  </Question>
  <Question>
    <QuestionIdentifier>likelytowin</QuestionIdentifier>
    <DisplayName>The Next Move</DisplayName>
    <IsRequired>true</IsRequired>
    <QuestionContent>
      <Text>
        How likely is it that player "X" will win this game?
      </Text>
    </QuestionContent>
    <AnswerSpecification>
      <SelectionAnswer>
        <StyleSuggestion>radiobutton</StyleSuggestion>
        <Selections>
          <Selection>
            <SelectionIdentifier>notlikely</SelectionIdentifier>
            <Text>Not likely</Text>
          </Selection>
          <Selection>
            <SelectionIdentifier>unsure</SelectionIdentifier>
            <Text>It could go either way</Text>
          </Selection>
          <Selection>
            <SelectionIdentifier>likely</SelectionIdentifier>
            <Text>Likely</Text>
          </Selection>
        </Selections>
      </SelectionAnswer>
    </AnswerSpecification>
  </Question>
</QuestionForm>

游戏01523,“X”来玩
你正在帮助决定抽签游戏的下一步行动。董事会如下所示:
形象
gif
http://tictactoe.amazon.com/game/01523/board.gif
游戏板,用“X”移动。
玩家“X”有下一步行动。
下一个移动
下一步
真的
在这个游戏中,玩家“X”的最佳移动坐标是多少?
C1
利克利托温
下一步
真的
玩家“X”赢得这场比赛的可能性有多大?
单选框
不太可能
不太可能
不确定
它可以走任何一条路
可能的
可能

我建议您使用JSON,伙计。它没有那么冗长。javascript有一些方法可以很好地处理它。如果您以后需要在类中使用XML,您可以编写一个简单的适配器。

var string=(即使XML肯定无效,也可以正常工作)。
var string = (<r><![CDATA[

   The text string goes here.  Since this is a XML CDATA section,
   stuff like <> work fine too, even if definitely invalid XML. 

]]></r>).toString();
]]>).toString();
我假设您的问题是如何获取准确的字符串,而不是从web服务或Ajax调用等检索到的字符串。虽然这是一个非常糟糕的想法,但有很多原因,要回答这个问题


在JavaScript中没有很好的方法可以做到这一点。一些浏览器通过在行的末尾放置
\
来支持行的延续,因此您可以执行以下操作:

var xml = '<QuestionForm xmlns="[the QuestionForm schema URL]">\
  <Overview>\
    <Title>Game 01523, "X" to play</Title>\
    ...';
var-xml='1〕\
\
游戏01523,“X”来玩\
...';
但这是不标准的,事实上与JS规范直接矛盾:

不能使用“LineTerminator”字符 以字符串文字显示,即使 前面有反斜杠

唯一真正可靠的方法是手动串接:

var xml = '<QuestionForm xmlns="[the QuestionForm schema URL]">' + "\n" +
'      <Overview>' + "\n" +
'        <Title>Game 01523, "X" to play</Title>' + "\n" +
'        ...';
var xml=''+“\n”+
''+“\n”+
'游戏01523,“X”来玩'+“\n”+
'        ...';
你可以这样把它弄得更整洁一些:

var xml = ['<QuestionForm xmlns="[the QuestionForm schema URL]">',
'      <Overview>',
'        <Title>Game 01523, "X" to play</Title>'
'        ...'].join("\n");
var xml=['',
'      ',
'游戏01523,“X”要玩'
“…”)。加入(“\n”);
但还是很糟糕


此外,使用所有这些方法,您必须转义任何单引号,即将
'
替换为
\'
。不过,乍一看,我在您的XML代码片段中没有看到任何内容,这很好。

我认为您可能希望将html/XML代码存储在javascript中,并显示在textarea或其他一些输入元素中。如果这是你的意图,这将帮助你


不要使用javascript变量,而是将代码存储在div中,并使用css ex:
display:none
隐藏。要显示代码时,可以使用
$('#div id').text()

到底有什么困难?您是否想知道格式(例如字符串或DOM对象)?还有什么吗?我想找到最简单的方法将其存储为字符串。谢谢,但这在Chrome中不起作用,给了我一个提示“这是什么疯狂的月亮语!?”!?当然不是JavaScript。你知道其他跨浏览器的方式可以将HTML包含到变量中吗?我说得对吗。JavaScript字符串中不能有“\n”?