有人能帮忙处理下面的错误吗?
它在做我想做的事,但每次滚动时都给我那个错误.我不知道为什么
未识别错误:语法错误,无法识别的表达式: div#
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')
}
});
}
});
});
发布于 2021-06-10 06:45:54
我认为jQuery选择器不承认语法"div#“。这可能适用于像"div#(id)“这样的css样式。但是在jQuery上,选择元素通常以$(‘’id‘)、$('.classname')或$(’元素‘)开头。
https://stackoverflow.com/questions/67915916
复制相似问题