【问题标题】:Add different text depending on URL with javascript使用 javascript 根据 URL 添加不同的文本
【发布时间】:2023-03-17 02:00:01
【问题描述】:

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

我需要根据 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

对于每个 Url,我需要在 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()"

【问题讨论】:

  • @mobilitydave 从地址栏或链接中获取此 URL 的位置

标签: javascript url text add


【解决方案1】:
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.
}

这应该会让你继续前进。

【讨论】:

  • URL 将来自页面的实际 URL。所以页面A加载我想要文本A显示或隐藏除Div A等之外的所有其他div。所以我有这个但它不起作用。
【解决方案2】:
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");
    }
    // my script start here
    var myURL = url.split('?');
    var myTexts = ["text1","text2","text3"];

    switch(myURL[0]){
     case "url1.html":
         $("#div_id").html(myTexts[0]);
     break;
     case "url2.html":
         $("#div_id").html(myTexts[1]);
     break;
    }
}

【讨论】:

  • mgraph 会给你的解决方案,现在会报告谢谢
  • 似乎不起作用..这可能是因为我的 URL 结尾是他的 ...html?sort=priceasc
  • 嗨,我现在正在使用它,但它首先加载页面然后更改 div 我可以在页面加载之前执行此操作吗 谢谢。[code]
  • 嗨尝试了上面的脚本,但没有做任何事情..对不起!编辑我的原始帖子,提供更多信息...谢谢
  • @mobilitydave 你换了吗case "url1.html":...
猜你喜欢
  • 2012-01-01
  • 2018-03-20
  • 2016-02-08
  • 2016-10-21
  • 2019-07-18
  • 2021-12-30
  • 2020-05-28
  • 1970-01-01
  • 2011-11-02
相关资源
最近更新 更多