2010-08-18 67 views
1
var theNewParagraph = document.createElement('p'); 
    var theBoldBit = document.createElement('b'); 
    var theBR = document.createElement('br'); 

    theNewParagraph.setAttribute('title','The test paragraph'); 
    var theText1 = document.createTextNode('This is a sample of some '); 
    var theText2 = document.createTextNode('HTML you might'); 
    var theText3 = document.createTextNode('have'); 
    var theText4 = document.createTextNode(' in your document'); 

    theBoldBit.appendChild(theText2); 
    theBoldBit.appendChild(theBR); 
    theBoldBit.appendChild(theText3); 

    theNewParagraph.appendChild(theText1); 
    theNewParagraph.appendChild(theBoldBit); 
    theNewParagraph.appendChild(theText4); 

    document.getElementById('someElementId').appendChild(theNewParagraph); 

另外,任何人都可以通過解釋這個幫助我嗎?我怎樣才能在HTML中運行這個?

+2

...在那裏在那裏一個問題嗎? :) – 2010-08-18 11:45:00

+0

@Nick Craver什麼? – Gladiator 2010-08-18 11:46:54

回答

2

這裏是一個合適的測試線束。以下內容粘貼到一個新的.html文件:

<html><head><script language="javascript"><!--// your javascript here: 
function _onload() 
{ 
    var theNewParagraph = document.createElement('p'); 
    var theBoldBit = document.createElement('b'); 
    var theBR = document.createElement('br'); 

    theNewParagraph.setAttribute('title','The test paragraph'); 

    var theText1 = document.createTextNode('This is a sample of some '); 
    var theText2 = document.createTextNode('HTML you might'); 
    var theText3 = document.createTextNode('have'); 
    var theText4 = document.createTextNode(' in your document'); 

    theBoldBit.appendChild(theText2); 
    theBoldBit.appendChild(theBR); 
    theBoldBit.appendChild(theText3); 

    theNewParagraph.appendChild(theText1); 
    theNewParagraph.appendChild(theBoldBit); 
    theNewParagraph.appendChild(theText4); 

    document.getElementById('someElementId').appendChild(theNewParagraph); 
} 
//--></script></head><body onload='_onload()' id='someElementId'></body></html> 
+0

//對於wat這個用法? 如果我想在我的數據庫中顯示名爲「Employee」的表。我應該怎麼做?如何使用dom?請解釋.. – Gladiator 2010-08-18 12:21:39

+0

@gladiator:* onload *在body元素中使用時,在瀏覽器完全加載文檔時,將該屬性的內容作爲javascript表達式執行。至於你的第二個問題,這完全適用於不同的線程。此外,你從一些JavaScript教程開始,而不是在深處跳躍,會好得多。 http://google.com/search?q=javascript+tutorial – 2010-08-18 12:36:36

4

將上述內容置於頁面底部的<script type="text/javascript">中,並確保文檔中有<div id="someElementId">

它在做什麼是創建一個新的<p>,<b><br>標記。然後在段落上設置標題,向所有標籤添加一些文本,最後將整個混亂添加到ID爲#someElementId的元素。您可以看到它in action here

5

你有什麼是JavaScript代碼片段。我添加註釋的代碼來解釋每個部分:

// Create 3 elements, a <p>, a <b> and a <br> 
var theNewParagraph = document.createElement('p'); 
var theBoldBit = document.createElement('b'); 
var theBR = document.createElement('br'); 

// Set the title attribute of the <p> element we created 
theNewParagraph.setAttribute('title','The test paragraph'); 

// Create 4 "text nodes", these appear as text when added to elements 
var theText1 = document.createTextNode('This is a sample of some '); 
var theText2 = document.createTextNode('HTML you might'); 
var theText3 = document.createTextNode('have'); 
var theText4 = document.createTextNode(' in your document'); 

/* Add the second text node, the <br> element and the 3rd text node to the 
    <b> element we created */ 
theBoldBit.appendChild(theText2); 
theBoldBit.appendChild(theBR); 
theBoldBit.appendChild(theText3); 

/* Add the first text node, the <b> element and the 4th text node to the 
    <p> element we created. All nodes are now descendants of the <p> */ 
theNewParagraph.appendChild(theText1); 
theNewParagraph.appendChild(theBoldBit); 
theNewParagraph.appendChild(theText4); 

/* Finally, add the <p> element to an element with an id attribute of 
    someElementId, so we can see all the content on our page */ 
document.getElementById('someElementId').appendChild(theNewParagraph); 

結果是下面的HTML作爲內容someElementId

<p title="The test paragraph">This is a sample of some <b>HTML you might<br> 
    have</b> in your document</p> 

其他人解釋如何將這個腳本添加到您的文檔使用<script>元素。

1

如何運行:

<head> 
    <script type="text/javascript"> 
     function CreateTestParagraph() { 
      var theNewParagraph = document.createElement('p'); 
      var theBoldBit = document.createElement('b'); 
      var theBR = document.createElement('br'); 

      theNewParagraph.setAttribute('title','The test paragraph'); 
      var theText1 = document.createTextNode('This is a sample of some '); 
      var theText2 = document.createTextNode('HTML you might'); 
      var theText3 = document.createTextNode('have'); 
      var theText4 = document.createTextNode(' in your document'); 

      theBoldBit.appendChild(theText2); 
      theBoldBit.appendChild(theBR); 
      theBoldBit.appendChild(theText3); 

      theNewParagraph.appendChild(theText1); 
      theNewParagraph.appendChild(theBoldBit); 
      theNewParagraph.appendChild(theText4); 

      document.getElementById('someElementId').appendChild(theNewParagraph); 
     } 
    </script> 
</head> 
<body onload="CreateTestParagraph()"> 
    <div id="someElementId"></div> 
</body> 

你CreateTestParagraph方法動態創建以下HTML內容:

<p title="The test paragraph">This is a sample of some <b>HTML you might<br>have</b> in your document</p> 

,並將該內容到someElementId元素。

相關鏈接:
createElement method
createTextNode method
appendChild method
getElementById method
onload event