尝试从Youtube API设置图像SCR时遇到问题
jQuery
$("#imgscr").attr("src", data.items[0].snippet.thumbnails.default.url);
html代码
<img src="#" id="duckBhaiimg" alt="Alternate Text" />
Json
"items": [
{
"kind": "youtube#channel",
"etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/8ENLhz-4x-I1Wub-o7UpmXo-rcY\"",
"id": "UC-lHJZR3Gqxm24_Vd_AJ5Yw",
"snippet": {
"title": "PewDiePie",
"description": "I make videos.",
"customUrl": "PewDiePie",
"publishedAt": "2010-04-29T10:54:00.000Z",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/a-/AAuE7mAPBVgUYqlLw9SvJyKAVWmgkQ2-KrkgSv4_5A=s88-mo-c-c0xffffffff-rj-k-no",
"width": 88,
"height": 88
},
"medium": {
"url": "https://yt3.ggpht.com/a-/AAuE7mAPBVgUYqlLw9SvJyKAVWmgkQ2-KrkgSv4_5A=s240-mo-c-c0xffffffff-rj-k-no",
"width": 240,
"height": 240
},
"high": {
"url": "https://yt3.ggpht.com/a-/AAuE7mAPBVgUYqlLw9SvJyKAVWmgkQ2-KrkgSv4_5A=s800-mo-c-c0xffffffff-rj-k-no",
"width": 800,
"height": 800
}
},
"localized": {
"title": "PewDiePie",
"description": "I make videos."
},
"country": "US"
}
}
]
}
答案 0 :(得分:1)
您拥有ID为<img src="#" id="duckBhaiimg" alt="Alternate Text" />
的这张图片duckBhaiimg
,并且在您的jQuery
中正在寻找ID imgscr
。
将代码更改为以下内容。
<img src="#" id="imgscr" alt="Alternate Text" />
$("#imgscr").attr("src", data.items[0].snippet.thumbnails.default.url);
const data = { items: [
{
"kind": "youtube#channel",
"etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/8ENLhz-4x-I1Wub-o7UpmXo-rcY\"",
"id": "UC-lHJZR3Gqxm24_Vd_AJ5Yw",
"snippet": {
"title": "PewDiePie",
"description": "I make videos.",
"customUrl": "PewDiePie",
"publishedAt": "2010-04-29T10:54:00.000Z",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/a-/AAuE7mAPBVgUYqlLw9SvJyKAVWmgkQ2-KrkgSv4_5A=s88-mo-c-c0xffffffff-rj-k-no",
"width": 88,
"height": 88
},
"medium": {
"url": "https://yt3.ggpht.com/a-/AAuE7mAPBVgUYqlLw9SvJyKAVWmgkQ2-KrkgSv4_5A=s240-mo-c-c0xffffffff-rj-k-no",
"width": 240,
"height": 240
},
"high": {
"url": "https://yt3.ggpht.com/a-/AAuE7mAPBVgUYqlLw9SvJyKAVWmgkQ2-KrkgSv4_5A=s800-mo-c-c0xffffffff-rj-k-no",
"width": 800,
"height": 800
}
},
"localized": {
"title": "PewDiePie",
"description": "I make videos."
},
"country": "US"
}
}
]
}
$("#imgscr").attr("src", data.items[0].snippet.thumbnails.default.url);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="#" id="imgscr" alt="Alternate Text" />