【问题标题】:how to change the background image of div using javascript?如何使用 javascript 更改 div 的背景图像?
【发布时间】:2021-12-31 16:12:35
【问题描述】:

这是我的代码

<div style="text-align:center;">
  <div class="ghor" id="a" onclick="chek_mark()"></div>
  function call
</div>
<script type="text/javascript">
  function chek_mark(){
   var el= document.getElementById("a").style.background-image;
   if (el.url("Black-Wallpaper.jpg"))  
   {
     el.url = "cross1.png";
    }
    else if(el.url("cross1.png"))
    {
      alert("<h1>This is working too.</h1>");
     }
   }
</script>

这里我想用 if else 条件来改变背景图片

this is the style-sheet
.ghor //this is the div class
{
    background-image: url('Black-Wallpaper.jpg');
    background-size: cover;
    border-radius: 5px;
    height: 100px;
    width: 100px;
    box-shadow: 2px 5px 7px 7px white;
    /*background-color: black;*/
    display:inline-block; 
}

我想改变'div'的背景图片,哪个类是'ghor'

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    试试这个:

    document.getElementById('a').style.backgroundImage="url(images/img.jpg)"; // specify the image path here
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      试试这个!

      var el = document.getElementById("a").style.backgroundImage;
      if(el == "url(Black-Wallpaper.jpg)") { // full value is provided
         el.style.backgroundImage = "url(/link/to_new_file.png)"; // change it
      }
      

      【讨论】:

      • 这只会获取文件名,还是也会获取路径?
      • @Avisari,它将获取background-image css 属性的值。如果提供了文件路径,则其值也将包括文件路径。否则只有图片!
      • '(/link/to_new_file.png)' 我应该在这里写什么
      • 它不工作 'var el = document.getElementById('a').backgroundImage; if (el == "url(Black-Wallpaper.jpg)") { //el.style.backgroundImage = "url(cross1.png)";警报(“它的工作”); }'
      • 请添加('a').style.backgrounImage 而不是简单的('a').backgroundImage。因为背景是对象样式的属性而不是对象的属性。
      【解决方案3】:

      你可以通过以下方式做到这一点

      第 1 步

         var imageUrl= "URL OF THE IMAGE HERE";
         var BackgroundColor="RED"; // what ever color you want
      

      用于改变BODY的背景

      document.body.style.backgroundImage=imageUrl  //changing bg image
      document.body.style.backgroundColor=BackgroundColor //changing bg color
      

      使用 ID 更改元素

      document.getElementById("ElementId").style.backgroundImage=imageUrl
      document.getElementById("ElementId").style.backgroundColor=BackgroundColor 
      

      对于具有相同类的元素

         var elements = document.getElementsByClassName("ClassName")
              for (var i = 0; i < elements.length; i++) {
                  elements[i].style.background=imageUrl;
              }
      

      【讨论】:

        【解决方案4】:

        HTML

        <body id="body">
        </body>
        

        JavaScript

        你也可以设置时间。

        //Initializing
        var i = 0;
        var images = []; //array
        var time = 3000; // time in millie seconds
        
        //images
        
        images[0] = "url(./Images/1.jpg)";
        images[1] = "url(./Images/2.jpg)";
        images[2] = "url(./Images/3.jpg)";
        //function
        
        function changeImage() {
            var el = document.getElementById('body');
            el.style.backgroundImage = images[i];
            if (i < images.length - 1) {
                i++;
            } else {
                i = 0;
            }
            setTimeout('changeImage()', time);
        }
        
        window.onload = changeImage;
        

        CSS

        overflow-x: hidden;
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100vh;
        background-repeat: no-repeat;
        background-position: center;
        

        【讨论】:

        • 请解释为什么这样做有效。谢谢!
        【解决方案5】:

            
        
         //Here is an example how to change background image of <div> element using javascript  by feching a new URL form some API.
         // let image teched from API: this is the image you want to put as new background !
        // thumbnail image:
        // data.picture.thumbnail
        //the HTML element whose background needs to be changes is given an id of #thumbnail.
        
         var thumbUrl = document.querySelector("#thumbnail");
             
        //using a function which will update data in the HTML - this is done
        //after parsing of the data fetched, I have omitted the steps used for parsing the data.
        function updateData(data){
           //data.picture.medium represents our new fetched data containing the URL fo the new image we want to use as background image.
            var newImage=data.picture.medium;
            thumbUrl.style.backgroundImage="url("+newImage+")";
        }

        【讨论】:

          【解决方案6】:

          //这里是一个示例,如何通过从某个 API 获取新 URL 来使用 javascript 更改元素的背景图像。

          // 让图像从 API 获取:这是您要放置为新背景的图像!

          //缩略图:

          //数据.图片.缩略图

          //需要更改背景的 HTML 元素的 id 为#thumbnail。

          var thumbUrl = document.querySelector("#thumbnail");
           
          

          //使用将更新 HTML 中的数据的函数 - 完成

          //解析获取的数据后,我省略了解析数据的步骤。

          function updateData(data){
          
            //data.picture.medium represents our new fetched data containing the URL fo the new image we want to use as background image.
            var newImage=data.picture.medium;
            thumbUrl.style.backgroundImage="url("+newImage+")";
          
          }
          

          【讨论】:

          • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
          【解决方案7】:

          你可以用 Jquery 做到这一点

          $('#div_id').css("backgroundImage","url(img_path)");
               
          

          【讨论】:

            猜你喜欢
            • 2016-05-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多