2

我想在设置页面上将 url 保存到 localStorage-。然后我需要一个超链接或一个按钮,点击后会转到该网址。尽量不使用 jquery。

<!doctype html>
<html>
<head>
<title>Local Storage URL</title>

<script type="text/javascript"> 
function save() {
    var myURL = document.getElementById('url');
    localStorage.setItem('url', myURL.value);
}

function load() {
    var storedValue = localStorage.getItem('url');
    if(storedValue) {
        document.getElementById('url').value = storedValue;
    }
}

</script>

</head>

<body onLoad="load()">
<input type="text" id="url" />
<input type="button" value="save" onclick="save()" />
<p>
<-- Here is where I am stuck below --><br>
<a href="('myUrl')">Link to localStorage Address</a>
</body>
</html>    
4

1 回答 1

1

你应该设置锚标签的href和innerHTML,

<a id="link" href="('myUrl')">Link to localStorage Address</a>

function load() {
    var storedValue = localStorage.getItem('url');
    var linkTag = document.getElementById('link');
    var urlInput = document.getElementById('url');
    if(storedValue) {
        urlInput.value = storedValue;
        linkTag.href = storedValue;
        linkTag.innerHTML = storedValue;
    }
}

演示

于 2012-05-15T21:36:07.407 回答