2011-09-27 47 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"; 
}