2012-03-27 84 views
0

我有這樣的功能:當設置一個變量,我jQuery的不能正常工作

<script type='text/javascript'> 
$('#clicktogohome').live('click', function(){ 
    history.pushState({}, '', this.href); 
    popstate(this.href); 
    return false; 
}); 
$('#listentosong').live('click', function(){ 
    history.pushState({}, '', this.href); 
    popstate(this.href); 
    return false; 
}); 

popstate = function(url){ 
    url = '/ajaxlinks/'+window.location.pathname.substr(1); 
    if (url == '/ajaxlinks/'){url = '/ajaxlinks/index.php'} 
    $('#ajaxloadcontent').load(url+"#ajaxloadcontent > *"); 
} 

window.onpopstate = function(event){ 
    popstate(window.location.href); 
    event.preventDefault(); 
} 
</script> 

這個功能是我的網站非常重要,完美的作品,當我不加入這一行:

if (url == '/ajaxlinks/'){url = '/ajaxlinks/index.php'} 

加入這一行,新的URL更新,背部和乖謬按鈕後仍然可以工作,但是,好像這行不工作,我的網頁上更新DIV:

$('#ajaxloadcontent').load(url+"#ajaxloadcontent > *"); 

這在jQuery中很常見,有誰知道我可以如何修復它。該函數可以在不設置變量「url」的情況下工作,但必須爲我使用該函數完成。有誰知道如何解決這一問題?謝謝!

+0

你爲什麼要追加'> *'到URL網址和選擇之間的空間? – 2012-03-27 19:57:23

+0

來自你的JS控制檯的任何錯誤? – halfer 2012-03-27 19:57:48

+0

@JamesJohnson:因爲在'.load'中的URL後面添加一個選擇器會告訴它只添加匹配選擇器的內容。 – 2012-03-27 19:59:51

回答

3

問題是url變量和字符串之間沒有空格。它應該,我想,是:

$('#ajaxloadcontent').load(url+" #ajaxloadcontent > *"); 

這是一個不正確的使用load()一個希望將URL和選擇,以在該網址發現頁面中找到。

我也會問你爲什麼使用> *選擇器;因爲僅使用#ajaxloadcontent將檢索元素本身(包括其後代元素)。

進一步的評論留給了這個問題:

GET http://www.pearlsquirrel.com/ajaxlinks/index.php 404 (Not Found)

且不說引用錯誤:

http://www.pearlsquirrel.com/:53Uncaught SyntaxError: Unexpected identifier http://www.pearlsquirrel.com/:365Uncaught SyntaxError: Unexpected end of input

That still does not work for me. If you want to see the problem that I am having, go here pearlsquirrel.com/listen.php?u=101 and click on the PearlSquirrel logo. The URL updates and yet the content does not load. This still occurs with the code above.

需要注意的是JavaScript控制檯報告是非常重要的參考:

+0

這仍然不適合我。如果您想查看我遇到的問題,請點擊http://www.pearlsquirrel.com/listen.php?u=101並點擊PearlSquirrel徽標。 URL更新,但內容不加載。這仍然發生在上面的代碼中。 – Eggo 2012-03-27 20:01:07

+0

如果load()不能找到你正在嘗試連接的頁面,它就不會工作,而***不能工作。 – 2012-03-27 20:04:55

+0

對不起,遲到了,我正在吃晚餐。感謝您向我展示,問題是我已將目錄中的文件重命名,但忘記更改爲代碼中的鏈接。對不起,感謝所有的幫助! – Eggo 2012-03-27 20:36:38

0

您需要.load.

$('#ajaxloadcontent').load(url+" #ajaxloadcontent > *");