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

时间:2011-02-26 06:48:14

标签: 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 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>

4 个答案:

答案 0 :(得分:9)

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


在JavaScript中没有真正好的方法。有些浏览器通过在行尾添加\来支持行继续,因此您可以执行以下操作: / p>

var xml = '<QuestionForm xmlns="[the QuestionForm schema URL]">\
  <Overview>\
    <Title>Game 01523, "X" to play</Title>\
    ...';

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

  

'LineTerminator'字符不能   出现在字符串文字中,即使   之前是反斜杠。

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

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

你可以这样做一点整洁:

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

但它仍然很糟糕。

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

答案 1 :(得分:4)

我想你可能想在javascript中存储html / xml代码并在textarea或其他一些输入元素中显示。如果这是你的意图,这将对你有所帮助。

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

答案 2 :(得分:0)

我建议你使用JSON,matey。它不那么冗长。并且javascript有很好的方法来处理它。如果您需要稍后在类中使用XML,则可以编写一个简单的适配器。

答案 3 :(得分:-9)

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();