嗨,我需要做一些我认为有点复杂的事情。

我需要根据URL将不同的文本添加到DIV中,我知道每种情况下URL是什么(共有6个),所以我需要javascript(jquery)来做到这一点,我只是自己知道该怎么做。

谢谢,戴夫。

 <script type = "text/javascript">
 function showDiv() {
 var url = window.location.href;
 if (/(Electric-Mobility)/i.test(url)) {
 document.getElementById("content_hide").style.display="none";
 }
 else {
 document.getElementById("content_hide").style.display="block";
 }
  }
 </script>
 <body onload = "showDiv()" >


这种工作,但不执行我想要的,并且在页面加载后也执行。

这些是网址
    ../brands/Electric-Mobility.html?sort=priceasc
    ../brands/Pride.html?sort=priceasc
    ../brands/Medical.html?sort=priceasc
    ../brands/Princey.html?sort=priceasc
    ../brands/NHC.html?sort=priceasc
    ../Roma.html?sort=priceasc

对于每个网址,我都需要在div #content_hide中使用不同的文本

希望这更清楚

谢谢
    新密码

    <script type="text/javscript">
    function showDiv() {
    var url = window.location.href;

    if (/(Electric-Mobility)/i.test(url)){

    // i changed your old script using jQuery
    $("#content_hide").css("display","none");
     } else {
   $("#content_hide").css("display","block");
   }


   var myURL = url.split('?');
    var myTexts = ["text1","text2","text3"];
     switch(myURL[0]){
        case "http://www.youngsmobility.co.uk/brands/Electric-Mobility.html":
         $("#content_hide").html(myTexts[0]);
     break;
      case "http://www.youngsmobility.co.uk/brands/Pride.html":
     $("#content_hide").html(myTexts[1]);
      break;
     }
     }
   </script>

    <body onload = "showDiv()" >


更新代码
            

     function showDiv() {
    var url = window.location.href;
    var myURL = url.split('?');
    var myTexts = ["text1","text2","text3"];
     switch(myURL[0]){
     case "www.youngsmobility.co.uk/brands/Electric-Mobility.html":
     $("#content_hide").html(myTexts[0]);
     break;
     case "www.youngsmobility.co.uk/brands/Pride.html":
     $("#content_hide").html(myTexts[1]);
     break;
    }
     }
     </script>

   <body onload = "showDiv()"

最佳答案

switch(location.href) {
    case "http://example.com/page1":
        // put text for page1 in div
        break;
    // add more cases as needed
    // optionally add a default case.
}


那应该让你走。

10-07 13:16