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

Mar*_*ark 2 javascript xml jquery 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 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>
Run Code Online (Sandbox Code Playgroud)

Dom*_*nic 9

我假设您的问题是如何获取确切的字符串,而不是从Web服务或Ajax调用中检索的字符串.虽然出于很多原因这是一个非常糟糕的主意,但要回答这个问题......


在JavaScript中没有真正好的方法.有些浏览器通过在行\的末尾放置一行来支持行继续,所以你会做类似的事情:

var xml = '<QuestionForm xmlns="[the QuestionForm schema URL]">\
  <Overview>\
    <Title>Game 01523, "X" to play</Title>\
    ...';
Run Code Online (Sandbox Code Playgroud)

但这是非标准的,事实上直接与JS规范相矛盾:

"LineTerminator"字符不能出现在字符串文字中,即使前面有反斜杠也是如此.

唯一真正可靠的方法是使用手动字符串连接:

var xml = '<QuestionForm xmlns="[the QuestionForm schema URL]">' + "\n" +
'      <Overview>' + "\n" +
'        <Title>Game 01523, "X" to play</Title>' + "\n" +
'        ...';
Run Code Online (Sandbox Code Playgroud)

你可以这样做一点整洁:

var xml = ['<QuestionForm xmlns="[the QuestionForm schema URL]">',
'      <Overview>',
'        <Title>Game 01523, "X" to play</Title>'
'        ...'].join("\n");
Run Code Online (Sandbox Code Playgroud)

但它仍然很糟糕.

此外,使用所有这些方法,您将不得不逃避任何单引号,即替换'\'.我乍一看你的XML片段中没有看到任何内容,这很好.