我需要访问href
属性并返回一堆img元素,其源代码为提取的href
属性。我如何在javascript或jQuery中执行此操作。
我做了这样的事,没有任何结果:
var myData = jQuery.parseJSON(jsonString);
alert(myData + "SDFS");
$(document).ready(function() {
var $grouplist = $('div#groups');
$.each(myData, function(index) {
$('<li>' + this.query.results.a[index].href+ '</li>').appendTo($grouplist);
});
JSON本身是:
{
"query": {
"count": 99,
"created": "2015-05-28T16:41:45Z",
"lang": "en-US",
"diagnostics": {
"publiclyCallable": "true",
"redirect": {
"from": "https://googledrive.com/host/0B-7mpdkiECAefkND6cy1tV3prSHc1b2Z3Vm9sSjNORDZfeHZRYldScUdZd1NGaUU/",
"status": "302",
"content": "https://1fd05575081cdaaf2fdc49e37444f3390503e229.googledrive.com/host/0B-7mpdkiEFTT3prSHc1b2Z3Vm9sSjNORDZfeHZRYldScUdZd1NGaUU/"
},
"url": [
{
"execution-start-time": "1",
"execution-stop-time": "1085",
"execution-time": "1084",
"content": "https://googledrive.com/host/0B-7mpdkiETXR6cy1tV3prSHc1b2Z3Vm9sSjNORDZfeHZRYldScUdZd1NGaUU/"
},
{
"execution-start-time": "1",
"execution-stop-time": "1085",
"execution-time": "1084",
"content": "https://googledrive.com/host/0B-7mpdkiECAefky1tV3prSHc1b2Z3Vm9sSjNORDZfeHZRYldScUdZd1NGaUU/"
},
{
"execution-start-time": "1",
"execution-stop-time": "1085",
"execution-time": "1084",
"content": "https://googledrive.com/host/0B-7mpdkiECAefkND3prSHc1b2Z3Vm9sSjNORDZfeHZRYldScUdZd1NGaUU/"
}
],
"user-time": "1108",
"service-time": "3234",
"build-version": "0.2.509"
},
"results": {
"a": [
{
"href": "/host/0B-7mpdkiECAefkNDUXFTTXR6cy1tV3prSHc1b2Z3Vm9sSjNORDZfeHZRYldScUdZd1NGaUU/CD%20RELEASE2014.jpg",
"content": "CD RELEASE.jpg"
},
{
"href": "/host/0B-7mpdkiECAefkNDUXFTTXR6cy1tV3prSHc1b2Z3Vm9sSjNORDZfeHZRYldScUdZd1NGaUU/dsc_6209.jpg",
"content": "dsc_6279.jpg"
},
{
"href": "/host/0B-7mpdkiECAefkNDUXFTTXR6cy1tV3prSHc1b2Z3Vm9sSjNORDZfeHZRYldScUdZd1NGaUU/dsc_6219_0.jpg",
"content": "dsc_6279_0.jpg"
},
{
"href": "/host/0B-7mpdkiECAefkNDUXFTTXR6cy1tV3prSHc1b2Z3Vm9sSjNORDZfeHZRYldScUdZd1NGaUU/dsc_620.jpg",
"content": "dsc_6280.jpg"
}
]
}
}
}
答案 0 :(得分:1)
现在这是一个普通的旧Javascript对象,因此您可以非常轻松地访问它。使用$ .each:
迭代你想要拉出的数组$.each(myData.query.results.a, function(elem) {
$('<li><img src="' + myData.query.results.a[elem].href + '"/></li>').appendTo($grouplist)
});
完整代码:
var myData = jQuery.parseJSON(jsonString);
alert(myData + "SDFS");
$(document).ready(function() {
var $grouplist = $('div#groups');
$.each(myData.query.results.a, function(elem) {
$('<li><img src="' + myData.query.results.a[elem].href + '"/></li>').appendTo($grouplist)
});
})
答案 1 :(得分:1)
尝试获取href属性
var obj=kk.query.results.a
for(var i = 0; i
的console.log(OBJ [I] .href);
}
答案 2 :(得分:0)
试试这个:
var myData = jQuery.parseJSON(jsonString);
alert(myData + "SDFS");
$(document).ready(function() {
var $grouplist = $('div#groups');
$.each(myData.results.a, function(obj) {
$('<li>' + obj.href + '</li>').appendTo($grouplist);
});