【问题标题】:Javascript not selector [duplicate]Javascript不是选择器[重复]
【发布时间】:2013-12-18 09:07:25
【问题描述】:

我是这个菜鸟所以我想问你是否知道如何选择除一个以外的所有元素。

这是脚本:

<script type="text/javascript">
    var jump = function (e) {
        //prevent the "normal" behaviour which would be a "hard" jump
        e.preventDefault();
        //Get the target
        var target = $(this).attr("href");
        //perform animated scrolling
        $('html,body').animate({
            //get top-position of target-element and set it as scroll target
            scrollTop: $(target).offset().top
            //scrolldelay:1 seconds
        }, 1000, function () {
            //attach the hash (#jumptarget) to the pageurl
            location.hash = target;
        });
    }
    $(document).ready(function () {
        $('a[href*=#]').bind("click", jump);
        return false;
    });
</script>
<!-- // end of smooth scrolling -->

我想对除 #myCarousel 之外的所有链接做同样的效果。问题是如何做到这一点以及需要在哪里包含:不是脚本中的选择器。

提前致谢。

【问题讨论】:

标签: javascript jquery twitter-bootstrap


【解决方案1】:

你可以使用.not(),比如:

$('a[href*=#]').not('#myCarousel').bind("click", jump);

【讨论】:

  • 这解决了我的问题。 $(document).ready(function() { //$('a[href*=#]').bind("click", jump); $('a[href*=#]').not( document.getElementsByClassName("carousel-control")).bind("click", jump); return false; });
【解决方案2】:

您可以在选择器本身中使用 :not。

$('a[href*=#]:not(#myCarousel)').bind("click", jump);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2016-11-09
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多