I want to save the content of the <a href>
In the localstorage and reuse it. How can i do that? I want to keep the link working. I want to use it to link to webapps (user provided) and i want to achieve that with the localstorage (i am building an online os For personal(and maybe) public use it won't be as big like eyeos or jolicloud).保存的<a href> in the localstorage
回答
If you want to save the whole link (including attributes):
<script>
function setLinks(){
var all_links = document.getElementById("container").innerHTML;
localStorage.setItem("savedLinkHTML", all_links);
}
function getLinks(){
var all_links = localStorage.getItem("savedLinkHTML");
if(all_links) document.getElementById("container").innerHTML = all_links;
}
window.onload = function(){
getLinks();
}
window.onunload = function(){
setLinks();
}
</script>
...
<div id="savedLinks"></div>
You can create your own functions to dynamically add more links (even images) to the container, which are automatically saved when leaving the page, and shown again when visiting the page.
在我看來,OP想要保存* href *屬性的值,而不是一大堆HTML。有一個[document.links](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-7068919)集合可以幫助解決這個問題。 – RobG
'document.links'是指文檔中的所有鏈接。 'document.getElementById(「savedLinks」)。getElementsByTagName(「a」)'只引用容器內的鏈接。調整代碼並不難,只存儲標籤和鏈接。我相信OP在保存數據方面存在問題。 –
使用'getElementsByTagName('a')'將返回所有A元素,這可能是錨點或鏈接(或兩者)。它也是一個函數調用,而不是直接的屬性訪問,所以速度較慢(這可能是不相關的,因爲用戶不會注意到它們之間的差異,除非有非常多的這些差異)。我的問題是存儲* herf *值,你的情況不同。似乎OP還沒有回來,所以在這一點上無所作爲。 :-) – RobG
內容爲什麼不搶URL
var url = window.location.href;
然後將其存儲在鑰匙(i)的值?我知道這聽起來很簡單,但這就是你問的問題......不是嗎?你只需要一個命名系統來檢索。
- 1. 相當於<a href="test" target="_blank"> in the php header() function
- 2. <a href in fireforx adds a %1B to the code but works fine with all other browsers?
- 3. 頁面跳轉點擊<a href> tag in IE, jquery is attached to the a href
- 4. DOMNodeInserted in the IE
- 5. 修改我下面<a href="http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator" rel="nofollow">the example in the docs</a>在$位置操作
- 6. <a href="#" adds localhost# to the address bar?
- 7. 替代<a href="#"> when the anchor tag only triggers a jQuery action without redirecting the user?
- 8. 確實foreach(<$IN>)sl the文件?
- 9. <a href=''> not working on the other pages
- 10. Perforce in the Cloud
- 11. PHP PDO製備IN()數組語句從<a href="http://www.php.net/manual/en/pdo.prepare.php" rel="nofollow">the PHP Manual</a>
- 12. <A Href> has a lot of empty space beneath the text
- 13. <a href> to external link returns link/Error in webapp, even though the server is not dead
- 14. 怎樣寫像<a href="#id"> which link to the same page in PHP?
- 15. 如何匹配'<a href=' in a string?
- 16. 顯示提交<a href> link onchange and pass the value of the input box changed
- 17. localstorage - 保存數組
- 18. 保存清單<EntityProxy> in GWT RequestFactory
- 19. map <A, set<A*>> vs set <A>其中A保存一組A *
- 20. Rafer to the other batch in the same folder in 2008 2008
- 21. About the Stanford CoreNLP in chinese model
- 22. 與<a href> tags in Javascript/JQuery
- 23. 內部href的<a tag now refreshes the page
- 24. 顯示保存的localStorage鍵
- 25. Java:remove-common-words-method in the API?
- 26. 顯示<A id> Inside placeholder in form
- 27. Couting characters,words,lenght of the words and total lenght in a sentence
- 28. 我的php echo聲明爲<a href="#"> is making the url visible
- 29. kendo mvc grid is splitting in in to 2 parts on the model binding of a list values
- 30. 如何用<a href.. in PHP
簽名/「Thank you,..」不應該添加到問題/回覆中(請參閱http://stackoverflow.com/faq#signatures)。 –