2016-07-25 36 views
0

HTML代碼window.location.hash差異寫的網址和標籤

<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 */ 
} 

工作的時候直接寫打字網址#搜尋:123
但是,窗口。 location.hash ='#search:'+字代碼不起作用。
更正確,默認頁面是石匠頁面。
*石工 - 我使用http://masonry.desandro.com/
也搜索頁面是石工頁面。默認頁
電網是休息。(位置復位),當搜索點擊後退按鈕單擊僅Android手機鉻
每個格子是其他定義
休息。

另外,

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

是正常的工作!只有

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

是中斷。

總之

,我知道區別寫網址,HREF標籤和window.location.hash

+0

標籤是https://www.w3.org/TR/html4/struct/links.html#adef-href我想知道window.location! T^T ...在每個大巴(SpiderMonkey,犀牛...等)或ECMA – Integral

回答

0

不幸的是,你是很難理解的。

我試過你的代碼在一個小提琴: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; 
}); 
+0

謝謝你的反饋!手機Chrome中的砌體打破(當使用腳本 - 窗口打開,位置href,位置哈希等...)
只有當正常工作。那麼我想知道差異腳本網址和HTML標記網址 – Integral