2011-02-26 73 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()