单击指向不同网站的超链接时显示的本地路径
local path displaying when I click hyperlink to different website
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" 我希望它只打开网站
您正在通过 JSON.stringify
传递 URL(这是一个字符串),这将它变成字符串的 JSON 表示(通过在周围添加 "
它)。
因为 URL 现在以 "
开头,所以它是一个亲戚 URL。
不要添加"
!不要使用 JSON.stringify
!
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" 我希望它只打开网站
您正在通过 JSON.stringify
传递 URL(这是一个字符串),这将它变成字符串的 JSON 表示(通过在周围添加 "
它)。
因为 URL 现在以 "
开头,所以它是一个亲戚 URL。
不要添加"
!不要使用 JSON.stringify
!