How to change the background image through the tag “selected”

前端 未结 2 1193
粉色の甜心
粉色の甜心 2021-01-25 22:25

I\'m trying to change the background image of the body (id = \"shelf\") using a drop-down menu:



        
相关标签:
2条回答
  • 2021-01-25 23:26

    It's working.

    function changeTheme() {
        var e = document.getElementById("themes");
        var theme = e.options[e.selectedIndex].value;
        document.getElementById("shelf").style.backgroundImage = "url(" + theme + ")";
    
    }
    
    0 讨论(0)
  • 2021-01-25 23:27

    What exactly is the behavior you are seeing?

    Try this:

    <html>
    <head>
    <script type="text/javascript">
    function changeTheme()
    {
    
      var e = document.getElementById("themes");
      var theme = e.options[e.selectedIndex].value;
      console.log(theme);
      document.getElementById("shelf").style.backgroundImage = "url("+theme+")";
    }  
    </script>
    </head>
    <body id="shelf">
    <select id="themes" onChange="changeTheme()">
      <option value="images/bg/default.png">Default</option>
      <option value="images/bg/oriental.png">Oriental</option>
      <option value="images/bg/office.png">Office</option>
      <option value="images/bg/old.png">Old</option>
    </select>
    </body>    
    </html>
    
    0 讨论(0)
提交回复
热议问题