[英]How do I fix "Uncaught Error: Syntax error, unrecognized expression: div#"?
任何人都可以帮助解决以下错误吗?
它正在做我想做的事,但每次滚动时都会给我那个错误......我不知道为什么
未捕获的错误:语法错误,无法识别的表达式: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')
}
});
}
});
});
我认为 jQuery 选择器无法识别语法“div#”。 这可能适用于 css 样式,例如“div#(id)”。 但在 jQuery 上,选择元素通常以 $('#id')、$('.classname') 或 $('elements') 开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.