2011-02-26 108 views
2

我想在JavaScript变量中存储一些HTML/XML标记。问题是文字比较大。例如,我如何将下面的XML片段存储在一个JavaScript变量中?在JavaScript变量中存储HTML或XML代码

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

究竟是什么困难?您是否想知道格式(例如,作为字符串还是DOM对象)?还有别的吗? – outis 2011-02-26 06:51:38

+0

我想找到最简单的方法来存储它作为一个字符串。 – Mark 2011-02-26 07:19:02

回答

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

谢谢,但这不适用于Chrome,给我“意想不到的令牌” – Mark 2011-02-26 07:29:22

+12

这是什么样的疯狂月亮语言!当然不是JavaScript。 – Domenic 2011-02-26 08:24:22

+0

感谢为我工作像一个魅力 – Mallik 2016-11-29 06:12:54

0

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

8

我想你的问题是如何采取精确的字符串,而不是一个你从Web服务或Ajax调用或类似的检索。虽然这是一个非常糟糕的主意有许多原因,回答这个问题......


没有真的很好用JavaScript这样做的方式。一些浏览器支持续行通过放置一个\在该行的结束,所以你会做这样的事情:

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片段中看到任何内容,这很好。

+0

你知道任何其他跨浏览器的方式,我们可以包含HTML到变量? – Pentium10 2012-09-13 13:57:01

+0

@ Pentium10否... – Domenic 2012-09-14 08:58:19

+0

我是否正确。 JavaScript字符串中不能有'\ n'? – omikron 2014-09-06 08:53:25

4

我想你可能想在JavaScript中存储HTML/XML代码并显示在textarea或其他输入元素。如果这是你的意图,这将帮助你。

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