2011-09-27 55 views
1

我有一些網頁將使用URL的路徑部分文本放置在頁面上,像這樣..的JavaScript文件撰寫VS的jQuery replaceWith

urlPath=window.location.pathname; 

urlPathArray = urlPath.split('/'); 


urlPath2=urlPathArray[2]; 
if (urlPath2 == "sometext") 
    { 
    document.write("Title for Some Text") 
    } 
else if (urlPath2 == "othertext") 
    { 
    document.write("Title for Other Text") 
    } 
else 
    { 
    document.write ("Generic Title") 
    } 

是否值得搞清楚如何做到這一點與jQuery和replaceWith函數?

+3

值得搞清楚如何做到這一點沒有'document.write'。 –

回答

0

我想創建一個div或其他一些容器像這樣的ID:

<div id="content"></div> 

,然後使用JavaScript DOM操作來填充它:

urlPath=window.location.pathname; 

urlPathArray = urlPath.split('/'); 

var oput = document.getElementById("content"); 

switch(urlPath2) 
{ 
    case "sometext": 
    oput.innerHTML = "Title for Some Text"; 
    break; 
    case "othertext": 
    oput.innerHTML = "Title for Some Other Text"; 
    break; 
    default: 
    oput.innerHTML = "Default Text"; 
}