2014-03-31 75 views
0

我有一個閱讀更多/更少jquery插件,我正在使用。其中一個設置(startOpen:真)允許內容在默認情況下,當你打開網頁進行擴展:將js函數添加到URL

<script> 
    $('#blog-post-2').readmore({ 
     maxHeight: 212, 
     startOpen: true 
    }); 
    $('#blog-post-1').readmore({ 
     maxHeight: 212 
    }); 
</script> 

我想要做的就是把startOpen:真正的功能到這個頁面的URL,像這樣:mywebsite.com/mypage.html & startOpen:true

原因是我想控制哪些用戶看到它擴大與否。

我不完全確定我會如何去實現這一點。

有沒有人有這個答案?

+1

ŧ ry使用location.hash,當startOpen == true時set location.hash =「#startOpen:true」 - > http://www.w3schools.com/jsref/prop_loc_hash.asp – lshettyl

回答

1

如果你打電話mypage.html#blog-post-1,你可以嘗試這樣的:

// Your settings here 
$('#blog-post-1, #blog-post-2').readmore({ 
    maxHeight: 212 
}); 

// Get the active hash 
var hash = window.location.hash; 

// Set the active element depending on the hash call 
$(hash).readmore({ 
    startOpen:true 
}); 
0

獲取這個jQuery URL解析插件:https://github.com/allmarkedup/purl

那麼,如果您的網址是mywebsite.com/mypage.html & startOpen:真

您添加到您的代碼

var startOpenSetting = $.url().param('startOpen'); 

$(document).ready(function(){ 

startOpen: startOpenSetting; 

)};