HTML
<div id="friendLinks" style="margin-top:15px; display:flex; flex-wrap:wrap;">
<span style="font-weight:bold;">友情链接:</span></div>
JS<script>
// 假设 JSON 文件的 URL
const jsonUrl = 'https://api.fenx.top/friends.json';
// 使用 fetch API 来加载 JSON 数据
fetch(jsonUrl)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json(); // 解析 JSON 数据
})
.then(data => {
const friendLinksDiv = document.getElementById('friendLinks');
data.forEach(item => {
const link = document.createElement('a');
link.href = item.url;
link.textContent = item.name;
link.style.color = 'white';
link.style.margin = '0 15px'; // 左右间距为 15px
link.target = '_blank';
friendLinksDiv.appendChild(link);
});
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
</script>
json[
{"name": "友情链接1", "url": "https://www.example1.com"},
{"name": "友情链接2", "url": "https://www.example2.com"},
{"name": "友情链接3", "url": "https://www.example3.com"}
]
|