这是我想用我的Javascript实现的,对不起,我是一个新手在Javascript [关闭]

ar5n3qh5  于 2023-01-04  发布在  Java
关注(0)|答案(2)|浏览(135)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
6个月前关闭。
Improve this question
对不起,我是新的javascirpt一直试图实现的结果上的图像附件。请帮助解决方案适用。
Here is what i want to achieve
下面是我的HTML代码:

<div class="col p-5">
              <div class="card" style="width: 18rem;">
                <img src="./img/connecting-to-go-of-elijah.jpg" class="card-img-top" alt="...">
                <div class="card-body">
                  <h5 class="card-title text-center">Connecting to God of Elijah</h5>
                  <p class="card-text text-center">Come and experience worship, word encounter, salvation, prayers, deliverance & prophetic declaration.</p>
                  <div class="row">
                    <div class="col-lg-12 text-center"><a href="#" class="btn btn-danger">WATCH SERMON</a></div>
                    <div class="col-lg-12 text-center pt-3">
                      <a href="#" ><img src="./img/film-solid.svg" width="10%" height="auto" class="p-1"/></a>
                      <a href="#" ><img src="./img/headphones-simple-solid.svg" width="10%" height="auto" class="p-1"/></a>
                      <a href="#" ><img src="./img/download-solid.svg" width="10%" height="auto" class="p-1"/></a>
                      <a href="#" ><img src="./img/book-solid.svg" width="10%" height="auto" class="p-1"/></a>
                    </div>
                  </div>
                </div>
              </div>
            </div>

下面是我的Javascript代码

function mFeeds()
{
    let headers = new Headers();

    headers.append('Content-Type', 'application/json');
    headers.append('Accept', 'application/json');

    headers.append('Access-Control-Allow-Origin', 'https://mfeeder.herokuapp.com/feed');
    headers.append('Access-Control-Allow-Credentials', 'true');

    var requestOptions = {
    method: 'GET',
    redirect: 'follow',
    headers: headers
    };

    fetch("https://mfeeder.herokuapp.com/feed", requestOptions)
    .then(response => response.text())
    .then(result => {
        let mResults = JSON.parse(result)
        for (let i = 0; i < 7; i++) {
            mTitle = mResults.feeds[i].text
            mDescription = mResults.feeds[i].description
            mLink = mResults.feeds[i].link

            console.log(mTitle, mDescription, mLink)
        }
        

    })
    .catch(error => console.log('error', error));

}

如何将结果mTitle、mDescirption、mLink从JavaScript填充到HTML页面?

zzwlnbp8

zzwlnbp81#

实现这一点的最佳方法是在for循环中创建元素,并将它们添加到包含所有卡片的容器中。2过程如下:1-创建一个变量来保存容器; 2-获取您的数据; 3-交互并将元素发送到负责创建卡的功能; 4-将此元素附加到容器。

function myCard(article) {
            return `
            <div class="card" style="width: 18rem;">
                <img src="${article.link}" class="card-img-top" alt="...">
                <div class="card-body">
                    <h5 class="card-title text-center">${article.text}</h5>
                    <p class="card-text text-center">${article.description}</p>
                    <div class="row">
                    <div class="col-lg-12 text-center"><a href="#" class="btn btn-danger">WATCH SERMON</a></div>
                    <div class="col-lg-12 text-center pt-3">
                        <a href="#" ><img src="./img/film-solid.svg" width="10%" height="auto" class="p-1"/></a>
                        <a href="#" ><img src="./img/headphones-simple-solid.svg" width="10%" height="auto" class="p-1"/></a>
                        <a href="#" ><img src="./img/download-solid.svg" width="10%" height="auto" class="p-1"/></a>
                        <a href="#" ><img src="./img/book-solid.svg" width="10%" height="auto" class="p-1"/></a>
                    </div>
                    </div>
                </div>
            </div>
            `
        }

        (function mFeeds()
        {
            // This is the container that holds all cards
            const container = document.querySelector('#card-main-container')
            let headers = new Headers();

            headers.append('Content-Type', 'application/json');
            headers.append('Accept', 'application/json');

            headers.append('Access-Control-Allow-Origin', 'https://mfeeder.herokuapp.com/feed');
            headers.append('Access-Control-Allow-Credentials', 'true');

            var requestOptions = {
            method: 'GET',
            redirect: 'follow',
            headers: headers
            };

            fetch("https://mfeeder.herokuapp.com/feed", requestOptions)
            .then(response => response.text())
            .then(result => {
                let mResults = JSON.parse(result)
                //console.log(mResults)
                mResults.feeds.forEach(element => {
                    //console.log(element)
                    let parser = new DOMParser();
                    const theCard = parser.parseFromString(myCard(element), 'text/html');
                    container.appendChild(theCard.firstChild)
                });

                for (let i = 0; i < 7; i++) {
                    mTitle = mResults.feeds[i].text
                    mDescription = mResults.feeds[i].description
                    mLink = mResults.feeds[i].link

                    //console.log(mTitle, mDescription, mLink)
                }
                

            })
            .catch(error => console.log('error', error));

        })()
<div class="col p-5" id="card-main-container">
  </div>
uqxowvwt

uqxowvwt2#

function mFeeds() {
  let headers = new Headers();

  headers.append('Content-Type', 'application/json');
  headers.append('Accept', 'application/json');

  headers.append('Access-Control-Allow-Origin', 'https://mfeeder.herokuapp.com/feed');
  headers.append('Access-Control-Allow-Credentials', 'true');

  var requestOptions = {
    method: 'GET',
    redirect: 'follow',
    headers: headers
  };

  fetch("https://mfeeder.herokuapp.com/feed", requestOptions)
    .then(response => response.text())
    .then(result => {
      let mResults = JSON.parse(result)
      for (let i = 0; i < 7; i++) {
        mTitle = mResults.feeds[i].text
        mDescription = mResults.feeds[i].description
        mLink = mResults.feeds[i].link

        //console.log(mTitle, mDescription, mLink)
        document.getElementById("title").innerHTML = mTitle
        document.getElementById("description").innerHTML = mDescription
        document.getElementById("url-link").href = mLink
      }

    })
    .catch(error => console.log('error', error));

}
mFeeds()
<div class="col p-5">
        <div class="card" style="width: 18rem;">
          <img src="./img/connecting-to-go-of-elijah.jpg" class="card-img-top" alt="...">
          <div class="card-body">
            <h5 class="card-title text-center" id="title"></h5>
            <p class="card-text text-center" id="description">Come and experience worship, word encounter, salvation, prayers, deliverance & prophetic declaration.</p>
            <div class="row">
              <div class="col-lg-12 text-center"><a id="url-link" href="#" class="btn btn-danger">WATCH SERMON</a></div>
              <div class="col-lg-12 text-center pt-3">
                <a href="#"><img src="./img/film-solid.svg" width="10%" height="auto" class="p-1" /></a>
                <a href="#"><img src="./img/headphones-simple-solid.svg" width="10%" height="auto" class="p-1" /></a>
                <a href="#"><img src="./img/download-solid.svg" width="10%" height="auto" class="p-1" /></a>
                <a href="#"><img src="./img/book-solid.svg" width="10%" height="auto" class="p-1" /></a>
              </div>
            </div>
          </div>
        </div>
      </div>

相关问题