首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何修正“未识别的错误:语法错误,无法识别的表达式: div#"?

如何修正“未识别的错误:语法错误,无法识别的表达式: div#"?
EN

Stack Overflow用户
提问于 2021-06-10 06:34:35
回答 1查看 2.6K关注 0票数 0

有人能帮忙处理下面的错误吗?

它在做我想做的事,但每次滚动时都给我那个错误.我不知道为什么

码页

未识别错误:语法错误,无法识别的表达式: div#

代码语言:javascript
运行
复制
jQuery(document).ready(function ($) {

function isOnScreen(elem) {
        // if the element doesn't exist, abort
        if (elem.length == 0) {
            return;
        }
        var $window = jQuery(window);
        var viewport_top = $window.scrollTop();
        var viewport_height = $window.height();
        var viewport_bottom = viewport_top + viewport_height;
        var $elem = jQuery(elem);
        var top = $elem.offset().top + (viewport_height / 2);
        var height = $elem.height();
        var bottom = top + height;

        return (top >= viewport_top && top < viewport_bottom);
    }


    // get data attricute > scroll into view of that attribute > if scrolled into view add class to link > remove class when scroll out

    $(document).scroll(function () {
        var width = $(window).width();

        var scrollPos = $(document).scrollTop();
        var windowHeight = $(window).height();

        if (width > 1200) {
            $('.header').each(function () {
                var _this = $(this);
                var _data_id = _this.attr('data-id');
                if (isOnScreen($('div#' + _data_id))) {
                    $('.header').removeClass('active')
                    _this.addClass('active')
                }
            });
        }
    });
  });
EN

回答 1

Stack Overflow用户

发布于 2021-06-10 06:45:54

我认为jQuery选择器不承认语法"div#“。这可能适用于像"div#(id)“这样的css样式。但是在jQuery上,选择元素通常以$(‘’id‘)、$('.classname')或$(’元素‘)开头。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67915916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档