我正在尝试从数据库中对某些数据进行排序。如果我从a标签中的href中删除了#pack,那么排序工作正常,但它返回到页面顶部。我需要它来到id包的页面部分。 这是HTML部分
<h1 id="pack">Packages</h1>
<div class="sort">
<p>Sort By <a href="index.php#pack?sort=htl"><i class="fa fa-sort-amount-desc" aria-hidden="true"></i> Price high to low</a> | <a href="index.php#pack?sort=lth"><i class="fa fa-sort-amount-asc" aria-hidden="true"></i> Price low to high</a></p>
</div>
我需要它返回id&#34; pack&#34;。 这是我用过的php代码。
if(isset($_GET['sort']))
{
$sort=$_GET['sort'];
if($sort=='htl')
{
$package_details=mysqli_query($connection,"SELECT * FROM package_details ORDER BY cost DESC");
}
if($sort=='lth')
{
$package_details=mysqli_query($connection,"SELECT * FROM package_details ORDER BY cost");
}
}
else
{
$package_details=mysqli_query($connection,"SELECT * FROM package_details");
}
UPDATE 删除平滑滚动js后它正在工作。如何在不删除的情况下执行此操作。这是代码。
// Adding Smooth Scroll with JQuery.......................................................
// Select all links with hashes
$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(function (event) {
// On-page links
if (
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') &&
location.hostname == this.hostname
) {
// Figure out element to scroll to
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
// Does a scroll target exist?
if (target.length) {
// Only prevent default if animation is actually gonna happen
event.preventDefault();
$('html, body').animate({
scrollTop: target.offset().top
}, 1000, function () {
// Callback after animation
// Must change focus!
var $target = $(target);
$target.focus();
if ($target.is(":focus")) { // Checking if the target was focused
return false;
} else {
$target.attr('tabindex', '-1'); // Adding tabindex for elements not focusable
$target.focus(); // Set focus again
};
});
}
}
});
//
&#13;
答案 0 :(得分:0)
通过删除哈希值,如果它适合你,那么我猜,它应该通过
工作 <a href="index.php?sort=htl#pack">
将哈希移动到URL的末尾,顺便说一句,您也可以到达页面的部分,例如
<h1 id="pack">Packages</h1>
<div class="sort">
<p>Sort By <a href="index.php?sort=htl#pack"><i class="fa fa-sort-amount-desc" aria-hidden="true"></i> Price high to low</a> | <a href="index.php#pack?sort=lth"><i class="fa fa-sort-amount-asc" aria-hidden="true"></i> Price low to high</a></p>
</div>