window.location.hash差异写入url和标签

window.location.hash difference write url and a tag

本文关键字:url 标签 location hash window      更新时间:2023-09-26

html代码

<div class="fakeSearch">
    <input id="searchInput" type="text" placeholder="Search">
    <img id="searchImg" class="clickAble" src="./img/search.png">
</div>

脚本

window.addEventListener("hashchange", ChangePage);
$('#searchImg').on('click', function(){
  var word = decodeURIComponent($('#searchInput').val());
  window.location.hash = '#search:'+word;
});
var ChangePage = function(){
  var myUrl = window.location.hash;
  var argStr = myUrl.split("''");
  argStr = argStr[0].split(":");
  argStr[0] = argStr[0].replace("#", "");
  $('#mainPlace > div').css('display','none');
  if(myUrl === ""){}
  ...
  else if(argStr[0]==='search'){
    $('#searchPage').css('display','block');
    InitLoadSearch(decodeURIComponent(argStr[1]));
  }
}
var InitLoadSearch = function(){
   /* call ajax data and print masonry */
}

编写直接键入url时工作#search:123
但是,window.location.hash='#search:'+word代码不起作用
更正确的是,默认页面是砖石页面
*砖石-我用http://masonry.desandro.com/
搜索页面也是砖石页面
默认页面的网格是间断的。(位置重置),在搜索时单击并返回按钮
仅限破解android移动chrome
每个网格都是其他定义的

此外,

<a href="#search:code"></a>

这是正常的工作!仅

window.location.hash = "#search:"+variable;

就是休息。

总之,我知道写url、标签hrefwindow.location.hash

的区别

不幸的是,您很难理解。

我试过你的密码:https://jsfiddle.net/3ouLyxds/它似乎起了作用。我只是更改了代码的顺序,否则根本不起作用:

var ChangePage = function(){
  var myUrl = window.location.hash;
  var argStr = myUrl.split("''");
  argStr = argStr[0].split(":");
  argStr[0] = argStr[0].replace("#", "");
  $('#mainPlace > div').css('display','none');
if(argStr[0]==='search'){
    $('#searchPage').css('display','block');
    InitLoadSearch(decodeURIComponent(argStr[1]));
  }
}
var InitLoadSearch = function(str){
   alert(str);
}
window.addEventListener("hashchange", ChangePage);
$('#searchImg').on('click', function(){
  var word = decodeURIComponent($('#searchInput').val());
  window.location.hash = '#search:'+word;
});