javascript document.write vs jQuery replaceWith

时间:2011-09-27 18:46:31

标签: javascript jquery

我有一些网页会使用网址的路径部分在网页上放置文字,就像这样..

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函数执行此操作?

1 个答案:

答案 0 :(得分: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";
}