问题
When I try to add this code to my webpage, whenever I click on the left or right arrow, it tries to redirect to "#carousel-example-generic", is there a way to do this without leaving my current web page?
Heres the code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link href="http://www.cssscript.com/wp-includes/css/sticky.css" rel="stylesheet" type="text/css">
<title>Slideshow Test</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<img src="/temp/0.jpg" alt="...">
<div class="carousel-caption" style="text-shadow: 5px 5px 10px #000000;">
</div>
</div>
<div class="item">
<img src="/temp/1.jpg" alt="...">
<div class="carousel-caption">
<h3>Caption Text</h3>
</div>
</div>
<div class="item">
<img src="/temp/2.jpg" alt="...">
<div class="carousel-caption">
<h3>Caption Text</h3>
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src='http://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js'></script>
</body>
</html>
回答1:
In addition to KJ Price's answer, you could do this (I assume you're using jQuery since you're clearly using Bootstrap):
$('.carousel-control').click(function(e){
e.preventDefault();
});
That way you can still keep the a tag, but the links won't trigger the new page load.
回答2:
Remove href="#carousel-example-generic"
from your <a>
. You may lose your "hand" when hovering though. So you may want to add href="javascript:void(0);"
instead.
As so:
<a class="left carousel-control" href="javascript:void(0);" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="javascript:void(0);" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
回答3:
replace the <a>
tag href
links with hash like:
<a class="left carousel-control" href="#" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
Or simply remove href
from the <a>
tag
回答4:
It's not a redirect, but an anchor to focus your carousel. It should scroll to <div id="carousel-example-generic" ...
.
Remove href="javascript:void(0);"
and add style="cursor:pointer"
来源:https://stackoverflow.com/questions/30597657/trying-to-add-carousel-to-webpage-without-redirecting