Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
包含javascript操作的URL_Javascript_Url - Fatal编程技术网

包含javascript操作的URL

包含javascript操作的URL,javascript,url,Javascript,Url,我有一个有很多div的页面,它们都是隐藏的。只有在单击某个链接时,才会显示相应的DIV 问题是: 是否有任何方法可以组成一个URL,其中包含在加载页面上单击链接时执行的指令 澄清: 这是一个包含多个div的页面(显示:无) 只显示链接 单击链接时,相应的DIV为display:block 该页面的URL为:www.mywebsite.com/OurServices 有没有一种方法,我可以生成或创建一个URL,就像 www.mywebsite.com/OurServices+JS单击链接以显示特定

我有一个有很多div的页面,它们都是隐藏的。只有在单击某个链接时,才会显示相应的DIV

问题是: 是否有任何方法可以组成一个URL,其中包含在加载页面上单击链接时执行的指令

澄清:

这是一个包含多个div的页面(显示:无)

只显示链接

单击链接时,相应的DIV为display:block

该页面的URL为:www.mywebsite.com/OurServices

有没有一种方法,我可以生成或创建一个URL,就像 www.mywebsite.com/OurServices+JS单击链接以显示特定服务


谢谢大家!

您应该使用如下URL变量

http://www.example.com/index.php?id=1&image=awesome.jpg
getQueryVariable("id");
这里有一个js函数来读取它们

function getQueryVariable(variable)
{
   var query = window.location.search.substring(1);
   var vars = query.split("&");
   for (var i=0;i<vars.length;i++) {
           var pair = vars[i].split("=");
           if(pair[0] == variable){return pair[1];}
   }
   return(false);
}
哪个将返回“1”或

将返回“awesome.jpg”

那就核对一下

if(getQueryVariable("id") == "1"){
      ///Add remove css class to hide show
}

您可以为此使用哈希值: www.mywebsite.com/OurServices#div1


当用户单击链接时,可以将哈希值附加到url

window.location.hash=value

加载时只需检查url值

例如


基于该值,只需显示和隐藏DIV。

这样我就可以向一些用户发送一个显示特定服务DIV的URL链接。。。谢谢您可以将
#
散列附加到URL,使其成为
www.mywebsite.com/OurServices#div1
为什么要删除散列,然后再添加回去?
if(getQueryVariable("id") == "1"){
      ///Add remove css class to hide show
}
$(document).ready(function(){  
  var hashValue = location.hash;  
  hashValue = hashValue.replace(/^#/, '');  
  //do something with the value here  
  $('#'+hashValue).show();
});
document.addEventListener( 'DOMContentLoaded', function () {
    var value = (window.location.hash).split('#')[1];
    // hide or show divs based on value
}, false );