jQuery从数据对象获取链接值 [英] Jquery get link values from data object
本文介绍了jQuery从数据对象获取链接值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一辈子都无法弄清楚如何访问该对象内的值.任何帮助将不胜感激
I can't for the life of me figure out how to access the values inside this object. Any help would be much apreciated
最终目标,一旦我可以访问链接,我将对其进行迭代,并将images-header元素后的img和img标签附加到dom.
End goal, once I can access the link, i'll iterate each and append and img tag to the dom after the images-header element.
JS使用json
$("#stack_name").focusout(function() {
var name = $(this).val();
// alert(name);
$.getJSON('/images.json?name='+ name, function(data) {
console.log("DATA: ", data);
// $('<p>Test</p>').appendTo('.images-header');
});
});
控制台日志输出:
DATA:
Object {data: Object}
data: Object
images: Array[4]
0: "http://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Connecticut_in_United_States.svg/270px-Connecticut_in_United_States.svg.png"
1: "http://www.enchantedlearning.com/usa/states/connecticut/map.GIF"
2: "https://familysearch.org/learn/wiki/en/images/0/01/Connecticut-county-map.gif"
3: "http://www.ct.gov/ecd/lib/ecd/20/14/state%2520of%2520connecticut%2520county%2520outline.jpg"
length: 4
__proto__: Array[0]
__proto__: Object
__proto__: Object
app-init.js?body=1:15
/images.json?name=connecticut
{
"data": {
"images": [
"http://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Connecticut_in_United_States.svg/270px-Connecticut_in_United_States.svg.png",
"http://www.enchantedlearning.com/usa/states/connecticut/map.GIF",
"https://familysearch.org/learn/wiki/en/images/0/01/Connecticut-county-map.gif",
"http://www.ct.gov/ecd/lib/ecd/20/14/state%2520of%2520connecticut%2520county%2520outline.jpg"
]
}
}
推荐答案
You can access images array like this, you have have to parse json string using $.parseJSON
img1 = jsonObj.data.Images[0];
Live Demo
您可以使用for循环遍历数组
You can iterate through array using for loop
for(i=0; i < jsonObj.data.images.length; i++)
{
alert(jsonObj.data.images[i]);
}
这篇关于jQuery从数据对象获取链接值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文