无需 JavaScript 的方式
您可以为此使用data-slide-to attribute,它接受从0开始的索引代表幻灯片编号。
使用数据属性轻松控制轮播的位置。 data-slide 接受关键字prev 或next,这会改变相对于其当前位置的幻灯片位置。或者,使用data-slide-to 将原始幻灯片索引传递给轮播data-slide-to="2",这会将幻灯片位置移动到以0 开头的特定索引。
只需为您的锚添加一个属性,然后就可以使用了。
<a href="#" data-slide-to="5">Go to slide #5</a>
感谢Cawas's comment on this answer 和Jonas's answer on this question。
老式的 JavaScript 方式
你需要使用.carousel(number)函数。
.carousel(数字)
将轮播循环到特定帧(从 0 开始,类似于
数组)。
查看bootstrap docs.
更具体的;
创建一个javascript函数来调用,它应该是这样的:
function goToSlide(number) {
$("#carousel").carousel(number);
}
然后,只需在您的链接上添加一个 onClick 事件。
<a href="#" onClick="javascript:goToSlide(5);">Go to slide #5</a>
从 URL 获取幻灯片信息
来源链接是www.example.com/models.php/4
您可以查看document.ready 上的网址,如果网址末尾有一些数据,您只需调用 goToSlide 函数即可。
var url = window.location.href;
var slide = url.substring(url.lastIndexOf('/')+1); // 4
// Remove the trailing slash if necessary
if( slide.indexOf("/") > -1 ) {
var slideRmvSlash = slide.replace('/','');
slide = parseInt(slideRmvSlash);
}
goToSlide(slide);