Javascript 在ajax之后重新定义owlcarouse
我正在做一个小项目,我需要调用一个API,并将结果添加到旋转木马 $'.owl carousel'.owl carousel{ 中:是的, 循环:对, 自动播放:对, 自动播放超时:1000, 自动播放暂停:对, }; 函数getChannels{ 返回['ninja','freecodecamp','aws']; } $document.readyfunction{ 让channels=getChannels.join','; $.ajax{ 网址:'https://api.twitch.tv/kraken/users?login=“+频道, 键入:“GET”, 数据类型:“json”, 成功:functiondata{ 设html=; const CHANNELS=data.users; CHANNELS.forEachfunctionchannel{ html=+channel.name+'Javascript 在ajax之后重新定义owlcarouse,javascript,owl-carousel,Javascript,Owl Carousel,我正在做一个小项目,我需要调用一个API,并将结果添加到旋转木马 $'.owl carousel'.owl carousel{ 中:是的, 循环:对, 自动播放:对, 自动播放超时:1000, 自动播放暂停:对, }; 函数getChannels{ 返回['ninja','freecodecamp','aws']; } $document.readyfunction{ 让channels=getChannels.join','; $.ajax{ 网址:'https://api.twitch.t
'+channel.bio+'
'; $.owl-carousel.appendhtml; }; }, 错误:函数{ 警惕‘嘘!’; }, 发送前:setHeader, }; }; 函数setHeaderxhr{ setRequestHeader'Accept','application/vnd.twitchtv.v5+json'; xhr.setRequestHeader'Client-ID','d0ovtk7831pgj75eaahflmefr1jrbx'; } 你好,世界! 卡片标题正文1
不确定为什么要用某些内容初始化转盘,然后用新内容替换其内容,但以下是需要更改的内容:
$(".owl-carousel").owlCarousel({
center: true,
loop: true,
autoplay: true,
autoplayTimeout: 1000,
autoplayHoverPause: true
});
function getChannels() {
return ["ninja", "freecodecamp", "aws"];
}
$(document).ready(() => {
const channels = getChannels().join(",");
$.ajax({
url: `https://api.twitch.tv/kraken/users?login=${channels}`,
type: "GET",
dataType: "json",
success(data) {
let html = "";
const CHANNELS = data.users;
// destroy the old carousel and cleanup the node
$(".owl-carousel").trigger("destroy.owl.carousel");
$(".owl-carousel").empty();
CHANNELS.forEach(channel => {
html = `<div class="item text-center"><div class="card"><img class="card-img-top" src="${
channel.logo
}" alt="${
channel.name
}"><div class="card-body"><h5 class="card-title">${
channel.name
}</h5><p class="card-text">${channel.bio}</p></div></div></div>`;
// append new HTML inside the loop
$(".owl-carousel").append(html);
});
// re-initialize the carousel outside the loop
$(".owl-carousel").owlCarousel({
center: true,
loop: true,
autoplay: true,
autoplayTimeout: 1000,
autoplayHoverPause: true
});
},
error() {
alert("boo!");
},
beforeSend: setHeader
});
});
function setHeader(xhr) {
xhr.setRequestHeader("Accept", "application/vnd.twitchtv.v5+json");
xhr.setRequestHeader("Client-ID", "d0ovtk7831pgj75eaahflmefr1jrbx");
}
是的,我做了,但结果是一样的。。。您可以在这里看到实现