13
4

2 に答える 2

37

要素にハッシュ属性がありません。href属性を取得し、javascriptのsubstr関数とindexOf関数を使用して、#で文字列を分割してみてください

var href = $(this).attr("href");
var hash = href.substr(href.indexOf("#"));

使用することもできます

$(this).prop("hash");
于 2012-05-31T19:55:51.167 に答える
4
$('a.js-hash').click(function() {

    // Store hash
    var hash = this.hash;

    // Using jQuery's animate() method to add smooth page scroll
    $('html, body').animate({
        scrollTop: $(hash).offset().top
    }, 1000, function() {

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
    });

    // Prevent default anchor click behavior
    return false;
});
于 2016-07-12T10:32:44.687 に答える