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 in fireforx adds a %1B to the code but works fine with all other browsers?
- 2. DOMNodeInserted in the IE
- 3. 替代<a href="#"> when the anchor tag only triggers a jQuery action without redirecting the user?
- 4. 確實foreach(<$IN>)sl the文件?
- 5. <a href=''> not working on the other pages
- 6. Perforce in the Cloud
- 7. <A Href> has a lot of empty space beneath the text
- 8. <a href> to external link returns link/Error in webapp, even though the server is not dead
- 9. 怎樣寫像<a href="#id"> which link to the same page in PHP?
- 10. About the Stanford CoreNLP in chinese model
- 11. 與<a href> tags in Javascript/JQuery
- 12. 內部href的<a tag now refreshes the page
- 13. 顯示<A id> Inside placeholder in form
- 14. Java:remove-common-words-method in the API?
- 15. Couting characters,words,lenght of the words and total lenght in a sentence
- 16. 我的php echo聲明爲<a href="#"> is making the url visible
- 17. 如何用<a href.. in PHP
- 18. WordPress的定製單product.php:<a data-full=... changing the main image?
- 19. .text&.attr in jquery not in the IE
- 20. Group Dispatcher.Invoke in the same function
- 21. localStorage不保存數據
- 22. 在localstorage上保存換行
- 23. localstorage如何保存div
- 24. 獲取使用<a href=... in Firefox
- 25. 中心<img /> + <a /> inside table and overflow auto in IE7
- 26. 如何處理<a href> inside a button in wordpress
- 27. 爲什麼要<a id="test"/> breaks out of the DOM chain?
- 28. 提取特定<a href> URLs out of the document
- 29. 保存對象文本的localStorage在AngularJS
- 30. PhoneGap的localStorage的斷電後保存?
簽名/「Thank you,..」不應該添加到問題/回覆中(請參閱http://stackoverflow.com/faq#signatures)。 –