0

如标题..如何让 src 成为我从另一个输入中得到的?她的是我的密码...

<script type="text/javascript">
    function newLink() {
        var myNewLink = document.getElementById("link");
        localStorage.setItem('link', myNewLink.value);
    }
    function newIcon() {
        var myNewIcon = document.getElementById("icon");
        localStorage.setItem('icon', myNewIcon.value);
    }
</script>

<form>
    <h1>lägg till länk</h1><br />
    <input type="text" id="link"><br />
    <input type="text" id="icon"><br />
    <button onClick="newLink(); newIcon();">lägg till länk</button>
</form>
<a href="#" onClick="location.href = localStorage.getItem('link')"><img src="localStorage.getItem('icon')" id="image"></a>
4

2 回答 2

0

假设您使用的是jQuery,您可以试试这个:

    $("#image").attr('src', localStorage.getItem('icon'));

你下面的代码,

      <script type="text/javascript">
            function newLink() {
                var myNewLink = document.getElementById("link");
                localStorage.setItem('link', myNewLink.value);
            }
            function newIcon() {
                var myNewIcon = document.getElementById("icon");
                localStorage.setItem('icon', myNewIcon.value);
            }
            $(function(){
                    $("#image").attr('src', localStorage.getItem('icon'));
            });


            function Redirect(){
                location.href = localStorage.getItem('link');

                return false;
            }

        </script>



        <form>
            <h1>lägg till länk</h1><br />
            <input type="text" id="link"><br />
            <input type="text" id="icon"><br />
            <button onClick="newLink(); newIcon();">lägg till länk</button>
        </form>
        <a href="#" onClick="Redirect();"><img src="" id="image"></a>
于 2013-11-09T10:39:30.170 回答
0
<script type="text/javascript">
        -----// your code
    function setLink(){
          location.href = localStorage.getItem('link');
   }
   window.onload = function(){
     document.getElementById("image").src = localStorage.getItem('icon');
  }

</script>

---- /// your code
<a href="#" onClick="setLink()"><img src="" id="image"></a>
于 2013-11-09T10:46:47.993 回答