将图像从图像文件夹动态添加到DIV框

发布于 2025-01-24 03:59:56 字数 903 浏览 0 评论 0原文

我正在尝试创建一个随机数生成器,其中基于与CSS文件夹在同一层次结构的IMG文件夹中选择图像的数字。但是,正在选择并显示图像。

<script type="text/javascript">

  var addObj = {

    add: function () {
      var box = document.getElementById('box');
      var box1 = document.getElementById('box1');
      var box2 = document.getElementById('box2');
      var pic = document.getElementById('pic')
      var x = Math.floor(Math.random() * 18) + 1;
      var y = Math.floor(Math.random() * 18) + 1;
      var z = Math.floor(Math.random() * 18) + 1;
      console.log(x);
      console.log(y);
      console.log(z);
    },

    EventListners: function () {
      var button = document.getElementById('rune');
      button.addEventListener('click', addObj.add, false);

      if (addObj.add.x = 1) {
        addObj.add.box = "url('./img/fa.jpg')";
      }else {
        console.log("n/a")
      }
    }

  };

addObj.EventListners();

I'm trying to create a random number generator where based on the number an image is chosen from the img folder that is on the same hierarchy as the CSS folder. However the image is being chosen and being displayed.

<script type="text/javascript">

  var addObj = {

    add: function () {
      var box = document.getElementById('box');
      var box1 = document.getElementById('box1');
      var box2 = document.getElementById('box2');
      var pic = document.getElementById('pic')
      var x = Math.floor(Math.random() * 18) + 1;
      var y = Math.floor(Math.random() * 18) + 1;
      var z = Math.floor(Math.random() * 18) + 1;
      console.log(x);
      console.log(y);
      console.log(z);
    },

    EventListners: function () {
      var button = document.getElementById('rune');
      button.addEventListener('click', addObj.add, false);

      if (addObj.add.x = 1) {
        addObj.add.box = "url('./img/fa.jpg')";
      }else {
        console.log("n/a")
      }
    }

  };

addObj.EventListners();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2025-01-31 03:59:56

您可以使用

  var box = document.getElementById('box');
  var box1 = document.getElementById('box1');
  var box2 = document.getElementById('box2');
  var pic = document.getElementById('pic')
  // this elements you can get globally
  var addObj = {

    add: function () {
      let x = Math.floor(Math.random() * 18) + 1;
      let y = Math.floor(Math.random() * 18) + 1;
      let z = Math.floor(Math.random() * 18) + 1;
      if (x == 1) { // the checker block you should add to the function at the eventlistener, the `=` is the assignment operator, the logically operator the `==` or the `===`
      box.src = './img/fa.jpg'; // the `box` variable is an image element, you can set the source with the `src` attribute
      }else {
        console.log("n/a")
      }
    },
    EventListners: function () {
      var button = document.getElementById('rune');
      button.addEventListener('click', addObj.add, false);

    }

  };

addObj.EventListners();

OOP非常好,但是起初您应该很好地了解该语言。

You can use

  var box = document.getElementById('box');
  var box1 = document.getElementById('box1');
  var box2 = document.getElementById('box2');
  var pic = document.getElementById('pic')
  // this elements you can get globally
  var addObj = {

    add: function () {
      let x = Math.floor(Math.random() * 18) + 1;
      let y = Math.floor(Math.random() * 18) + 1;
      let z = Math.floor(Math.random() * 18) + 1;
      if (x == 1) { // the checker block you should add to the function at the eventlistener, the `=` is the assignment operator, the logically operator the `==` or the `===`
      box.src = './img/fa.jpg'; // the `box` variable is an image element, you can set the source with the `src` attribute
      }else {
        console.log("n/a")
      }
    },
    EventListners: function () {
      var button = document.getElementById('rune');
      button.addEventListener('click', addObj.add, false);

    }

  };

addObj.EventListners();

The OOP is very nice thing, but at first you should know the language well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文