2013-10-04 38 views
1

我想製作一個網頁,從一個播客的XML文件併產生一個所有標題和直播鏈接到播客列表。但大多數情況下,這是可行的,但是當我嘗試用CSS添加樣式到頁面時,它就會消失,所以頁面就像沒有任何樣式適用於它一樣。Javascript創建divs刪除所有的CSS

這是一段Javascript我正在使用寫在頁面上的列表。

var podcasts; 
var titles = new Array; 
var urls = new Array; 

var yogpod = function() { 

    document.write("<h1> YogPod </h1>"); 

    var xml = loadXMLDoc("http://localhost/yogpod/yogpod.xml"); 
    podcasts = xml.getElementsByTagName("item"); 

    //Extract info into arrays for ease of use 
    for (i=0; i<podcasts.length; i++) { titles[i] = podcasts[i].children[0].textContent   } 
    for (i=0; i<podcasts.length; i++) { urls[i] = podcasts[i].children[6].getAttribute('url') } 


    //Make podcast 1 first 
    titles.reverse(); 
    urls.reverse(); 

    //Write podcasts and URLs on page 
    for (i=0; i<podcasts.length; i++) 
    { 
     var div = document.createElement('div'); 
     //div.setAttribute('class', 'podcast'); 
     div.className='podcast'; 
     var titleText = document.createTextNode(titles[i]); 
     var urlText = document.createTextNode(urls[i]); 

     div.appendChild(titleText); 
     div.appendChild(document.createElement('br')); 
     div.appendChild(urlText); 
     div.appendChild(document.createElement('br')); 

     var a = document.getElementsByTagName('body')[0]; 
     a.appendChild(div); 
    } 
} 

這就是結果是什麼,Chrome瀏覽器認爲該網頁是什麼。 Markup

編輯:我除去document.write("<h1> YogPod </h1>")線和它固定它。誰能告訴我爲什麼這是?

+0

結果標記是什麼樣的? – kunalbhat

+0

添加了一張圖片 – frasertmay

+0

我唯一能想到的就是你可能想改變'div.setAttribute('class','podcast');'div.className ='podcast';' –

回答

3

當你做一個document.write後的頁面加載,將其寫入指定的內容之前清空文件。將document.write替換爲document.createElement("h1")document.body.appendChild(...)

0
 // you have to create new element and new textNode 


var s = document.createElement('h1'); 
var t = document.createTextNode('yodu..'); 

var headr = s.appendChild(t); 
+0

不幸的是,這是行不通的。 – frasertmay