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)。 –