Javascript目标ahref和类

Javascript target ahref and class

本文关键字:和类 ahref 目标 Javascript      更新时间:2023-09-26

我试图让JS的一个片段瞄准这个HMTL。我试着在javascript中输入这个类,但认为这可能是错误的方法

HTML

<li><a href="#contentm" class="tablet">Projects.t</a></li>

JS

$("a[href='#contentm .tablet']").click(function () {
    $("html, body").animate({
        scrollTop: $("body").scrollTop() == 0 ? 500 : 0
    }, "slow");
    return false;
});
$("a.tablet[href='#contentm']").click(function () {
$("html, body").animate({
    scrollTop: $("body").scrollTop() == 0 ? 500 : 0
}, "slow");
return false;
});

你的订单有点乱

$("a[href='#contentm'].tablet").click(function () {
    $("html, body").animate({
        scrollTop: $("body").scrollTop() == 0 ? 500 : 0
    }, "slow");
    return false;
});

我想这就是你想要的。

try $("a.tablet[href='#contentm']")