【问题标题】:local path displaying when I click hyperlink to different website单击指向不同网站的超链接时显示的本地路径
【发布时间】:2020-07-11 03:00:26
【问题描述】:
fetch(www.gnewsapi.com/news/someID).then(response =>
newsurl.href = JSON.stringify(data.articles[0].url)
fetch('https://gnews.io/api/v3/search?q=platformer&token=642h462loljk').then(function (response) {
    return response.json();
  }).then(data => {
    newstitle.textContent = JSON.stringify(data.articles[0].title),
    newsdetails.textContent = JSON.stringify(data.articles[0].description),
    **newsurl.href = JSON.stringify(data.articles[0].url)**
  }

HTML:

<div class="card">
      <img class="card-img-top" id="news-img" src="" alt="Card image cap">
      <div class="card-body">
          <h5 class="card-title" id="news-title">News</h5>
          <p class="card-text" id="news-details">Download it</p>
          <a id="news-url" href="#" class="btn btn-primary">Read more</a>
      </div>
</div>

问题是当我点击超链接按钮时,它会打开 User/Me/Documents"https://www.androidpolice.com" 我希望它只打开网站

【问题讨论】:

    标签: javascript html href


    【解决方案1】:

    您通过 JSON.stringify 传递 URL(这是一个字符串),这会将其转换为字符串的 JSON 表示(通过在其周围添加 ")。

    由于 URL 现在以 " 开头,因此它是一个相对 URL。

    不要添加"!不要使用JSON.stringify

    【讨论】:

      猜你喜欢
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      相关资源
      最近更新 更多