如何附加作为一个
    • ;
  • 发布于 2025-01-15 15:31:33 字数 2339 浏览 6 评论 0原文

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

    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);
                });
    

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

    I've created a forEach loop that is collecting the urls of 10 gifs and I want to append them to a I currently have in my html on line 33.
    I have to display the gifs on the page.
    I'm currently getting a list of the urls.
    Is there a way I can have a list of images on the page keeping most of the code I currently have?
    From line 55 down is my forEach loop where I am trying to 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>

    I've edited to end to:

    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);
                });
    

    and the output is a list of broken images.

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

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

    发布评论

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

    评论(3

    顾忌 2025-01-22 15:31:33

    您需要一个新的 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>

    You need both a new li and img element. And if you would like to present the GIF itself you should set the src attribute of the image to 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>

    遇见了你 2025-01-22 15:31:33

    像这样的东西应该有效:

    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);
    });
    

    Something like this should work:

    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);
    });
    
    桃气十足 2025-01-22 15:31:33

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

    参考链接:
    https://developer.mozilla.org/en-US/文档/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>

    You can easily append image with help of Template literals and HTML DOM insertAdjacentHTML() Method. I hope below snippet will help you a lot.

    Reference 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>

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