javascript document.write vs jQuery replaceWith



我有一些页面将使用URL的路径部分在页面上放置文本,如…

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函数做到这一点?

我将创建一个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";
}

最新更新