因此,我正在尝试建立一个网站,在该网站上我可以每天更改图片。我需要将图像源设置为变量imageurl的帮助。同样,使变量imageurl为全局变量。

让我澄清一下,我想要它,以便它将变量imageurl设置为由提示设置的链接,然后将图像设置为imageurl

<!DOCTYPE html>
<html>

<head>
    <div align="center">
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>picture of the day</title>
         <h1>Jere's picture of the day</h1>
        <link href="style.css" rel="stylesheet" type="text/css">
    </div>
</head>

<body>
    <style>
        div {
            background-color: #8000ff;
            color: #ffffff;
            padding: 20px;
        }
    </style>
    <script src="code.js" type="text/javascript" charset="utf-8"></script>

    <script>
        function myFunction() {
            isitjere = prompt("Password?");
            if (isitjere === "thedog")
                alert("password:" + isitjere + " " + "has been accepted");
            editimage = prompt("do you want to change the image?(type yes or no)");
            if (editimage === "yes")
                imageurl = prompt("paste the image url(if you dont have a url for it you can upload it and paste the upload link)");
        }
    </script>
    <button onclick="myFunction()">edit</button>
    <img src=imageurl>
    </div>
</body>
</html>

最佳答案

不知道你的意图是什么..但这里的代码片段工作:)
http://codepen.io/f7o/pen/VjpjOW

<head>

</head>

<body>
  <script>
    function myFunction() {
       var isitjere = prompt("Password?");
                if (isitjere === "thedog")
                    alert("password:" + isitjere + " " + "has been accepted");
                var editimage = prompt("do you want to change the image?(type yes or no)");
                if (editimage === "yes")
                    var imageurl = prompt("paste the image url(if you dont have a url for it you can upload it and paste the upload link)");
      console.log(imageurl);
    $("#pic").attr('src',imageurl);
    }
  </script>
  <button onclick="myFunction()">edit</button>

  <div align="center">
    <title>picture of the day</title>
    <h1>Jere's picture of the day</h1>
  </div>
  <img id="pic" src="https://www.google.com/images/nav_logo242.png">

</body>

</html>

10-06 04:35
查看更多