如何将 <img> 作为 <li> 附加到 <ul>

How to append <img> as a <li> to a <ul>

我创建了一个 forEach 循环,它正在收集 10 个 gif 的 url,我想将它们附加到我目前在第 33 行的 html 中。 我必须在页面上显示 gif。 我目前正在获取网址列表。 有没有办法让我在页面上有一个图像列表来保留我目前拥有的大部分代码? 从第 55 行开始是我的 forEach 循环,我试图在其中添加 appendChild.

const button = document.querySelector("#search-btn");
button.addEventListener("click", (event) => {
  event.preventDefault();

  const giffTitle = document.querySelector("#giffTitle").value;

  axios;
  const giffSection = document.querySelector(".giffs");
  const giffList = document.querySelector(".list");

  axios
    .get(
      `https://api.giphy.com/v1/gifs/search?api_key=iVwoS0brONsmkA6sWVqHgJ9D7g2WYDmv&q=${giffTitle}&limit=10&offset=0&rating=g&lang=en`
    )
    .then((res) => {
      const giff = res.data;
      // const giffUrl = giff.data[0].embed_url;
      const giffUrl = giff.data;
      console.log(giffUrl);

      giffUrl.forEach(function(singleGiff) {
        let img = document.createElement("li");
        img.textContent = singleGiff.embed_url;
        giffList.appendChild(img);
        console.log(singleGiff.embed_url);
      });
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.26.0/axios.min.js" integrity="sha512-bPh3uwgU5qEMipS/VOmRqynnMXGGSRv+72H/N260MQeXZIK4PG48401Bsby9Nq5P5fz7hy5UGNmC/W1Z51h2GQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<h1>Giffs</h1>
<form action="">
  <label for="">Search</label>
  <input type="text" id="giffTitle" />
  <button id="search-btn">Search</button>
</form>
<iframe id="iframe" src="${giffUrl}" width="480" height="270" frameborder="0" class="giphy-embed" allowfullscreen></iframe>

<section class="giffs"></section>
<ul class="list"></ul>

我已编辑为结束:

giffUrl.forEach(function (singleGiff) {
              let img = document.createElement("img");
              img.src = singleGiff.embed_url;
              let list = document.createElement("li");

              list.appendChild(img);
              giffList.appendChild(list);
              console.log(singleGiff.embed_url);
            });

并且输出是损坏图像的列表。

像这样的东西应该可以工作:

giffUrl.forEach(function (singleGiff) {
    let img = document.createElement("img");
    img.src = singleGiff.embed_url;
    let listElement = document.createElement("li");
    listElement.appendChild(img);
    giffList.appendChild(listElement);
    console.log(singleGiff.embed_url);
});

您可以借助 模板文字 HTML DOM insertAdjacentHTML() 方法轻松附加图像。希望下面的代码片段能对您有所帮助。

参考Link:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals https://www.w3schools.com/jsref/met_node_insertadjacenthtml.asp

const button = document.querySelector("#search-btn");
button.addEventListener("click", (event) => {
    event.preventDefault();

    const giffTitle = document.querySelector("#giffTitle").value;

    axios;
    const giffSection = document.querySelector(".giffs");
    const giffList = document.querySelector(".list");

    axios.get(
        `https://api.giphy.com/v1/gifs/search?api_key=iVwoS0brONsmkA6sWVqHgJ9D7g2WYDmv&q=${giffTitle}&limit=10&offset=0&rating=g&lang=en`
    )
    .then((res) => {
        const giff = res.data;
        // const giffUrl = giff.data[0].embed_url;
        const giffUrl = giff.data;
        // console.log(giffUrl);
giffList.innerHTML = ''; // empty <ul> before listing
        giffUrl.forEach(function (singleGiff) {
            console.log(singleGiff)
            giffList.insertAdjacentHTML('beforeend', `
                <li>
                    <img src="${singleGiff.images.downsized.url}">
                </li>
            `)
        });
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.26.0/axios.min.js"
        integrity="sha512-bPh3uwgU5qEMipS/VOmRqynnMXGGSRv+72H/N260MQeXZIK4PG48401Bsby9Nq5P5fz7hy5UGNmC/W1Z51h2GQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    
<h1>Giffs</h1>
<form action="">
    <label for="">Search</label>
    <input type="text" id="giffTitle" />
    <button id="search-btn">Search</button>
</form>
<section class="giffs"></section>
<ul class="list"></ul>

您需要新的 liimg 元素。如果您想展示 GIF 本身,您应该将图像的 src 属性设置为 singleGiff.images.downsized.url.

const button = document.querySelector("#search-btn");
button.addEventListener("click", (event) => {
  event.preventDefault();

  const giffTitle = document.querySelector("#giffTitle").value;

  axios;
  const giffSection = document.querySelector(".giffs");
  const giffList = document.querySelector(".list");

  axios
    .get(
      `https://api.giphy.com/v1/gifs/search?api_key=iVwoS0brONsmkA6sWVqHgJ9D7g2WYDmv&q=${giffTitle}&limit=10&offset=0&rating=g&lang=en`
    )
    .then((res) => {
      const giff = res.data;
      // const giffUrl = giff.data[0].embed_url;
      const giffUrl = giff.data;
      //console.log(giffUrl);

      giffUrl.forEach(function(singleGiff) {
        let li = document.createElement("li");
        let img = document.createElement("img");
        img.src = singleGiff.images.downsized.url;
        li.appendChild(img);
        giffList.appendChild(li);
      });
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.26.0/axios.min.js" integrity="sha512-bPh3uwgU5qEMipS/VOmRqynnMXGGSRv+72H/N260MQeXZIK4PG48401Bsby9Nq5P5fz7hy5UGNmC/W1Z51h2GQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<h1>Giffs</h1>
<form action="">
  <label for="">Search</label>
  <input type="text" id="giffTitle" />
  <button id="search-btn">Search</button>
</form>
<iframe id="iframe" src="${giffUrl}" width="480" height="270" frameborder="0" class="giphy-embed" allowfullscreen></iframe>

<section class="giffs"></section>
<ul class="list"></ul>