通过从 JSON 文件中获取 URL 来更改 link 的 href 值 (jQuery)
Change href value of a link by fetching URL from JSON file (jQuery)
我有一个非常简单的 .JSON 文件,名为 'screen.json',如下所示:
[
{
"link": "http://localhost:8888/screen/screen1.png"
}
]
而且很简单HTML link:
<a id="screen" href="#"></a>
我想做的是,在页面加载时,使用 jQuery,从 .JSON 文件中获取 "link" 值并将其放入 href=""
# 符号所在的标签。
$(document).ready(function() {
}
我知道它会涉及上面 jQuery 中的 ajax 调用,以从 .json 文件中获取字符串,然后将其放入 'href=""'标签,但我不知道该怎么做。
有人能帮帮我吗?干杯! :)
$(document).ready(function(e) {
$.get(url, function(response) {
//response = JSON.parse(response);//use this in case you are not getting json in response to parse response
//Change href Value use attr()
$('#screen').attr('href', response[0]['link']);
});
});
我有一个非常简单的 .JSON 文件,名为 'screen.json',如下所示:
[
{
"link": "http://localhost:8888/screen/screen1.png"
}
]
而且很简单HTML link:
<a id="screen" href="#"></a>
我想做的是,在页面加载时,使用 jQuery,从 .JSON 文件中获取 "link" 值并将其放入 href=""
# 符号所在的标签。
$(document).ready(function() {
}
我知道它会涉及上面 jQuery 中的 ajax 调用,以从 .json 文件中获取字符串,然后将其放入 'href=""'标签,但我不知道该怎么做。
有人能帮帮我吗?干杯! :)
$(document).ready(function(e) {
$.get(url, function(response) {
//response = JSON.parse(response);//use this in case you are not getting json in response to parse response
//Change href Value use attr()
$('#screen').attr('href', response[0]['link']);
});
});