语法错误,无法识别的表达式,未显示 a[href 和画布
Syntax error, unrecognized expression with a[href and canvas not displaying
我在Bootstrap和jQuery上遇到了一个奇怪的问题。
我正在使用以下jQuery脚本在单击锚点和使用浏览器后退按钮返回页面顶部(删除 #anchorName 扩展名)时获得平滑滚动:
$(function() {
$('a[href*=#]:not([href=#])').click(function(event) {
event.preventDefault();
if (location.pathname.replace(/^'//,'') == this.pathname.replace(/^'//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
var hash = this.hash; // 'this' will not be in scope in callback
$('html,body').animate({ scrollTop: target.offset().top - 55}, 300, function() {
href = window.location.href;
history.pushState({page:href}, null, href.split('#')[0]+hash);
});
return false;
}
}
});
$(window).bind('popstate', function(event) {
var state = event.originalEvent.state;
var target = window.location.href.split('#');
var href = target[0];
if (state) {
$('html,body').animate({ scrollTop: 0 }, 300, function() {
history.replaceState({}, null, href);
})
}
});
window.onload = function() {
history.replaceState({ path: window.location.href }, '');
}
});
如果我使用双引号 ( $('a[href*="#"]:not([href="#"])')
) 上面的脚本运行良好,但我测试的页面包含 HTML5 画布并且此画布不显示。
另一方面,如果我不使用双引号($('a[href*=#]:not([href=#])')
)),"锚点"功能不起作用(我没有平滑滚动)并显示 HTML5 画布。
我在论坛上看到解决方案可能是:
1) double quoting :
$('a[href*="#"]:not([href="#"])')
或:
2) Unescape # character
$('a[href*=''#]:not([href=''#])')
我尝试了这些解决方案,但没有一个有效。
使用jQuery 1.12.0,我收到以下错误:
Error: Syntax error, unrecognized expression: a[href*=#]:not([href=#])
(代表问题作者发布答案以将其移动到答案空间)。
我自己解决了。最初,我的代码是:
$(function() {
$('a[href*=#]:not([href=#])').click(function(event) {
event.preventDefault();
if (location.pathname.replace(/^'//,'') == this.pathname.replace(/^'//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
var hash = this.hash; // 'this' will not be in scope in callback
$('html,body').animate({ scrollTop: target.offset().top - 55}, 300, function() {
href = window.location.href;
history.pushState({page:href}, null, href.split('#')[0]+hash);
});
return false;
}
}
});
$(window).bind('popstate', function(event) {
var state = event.originalEvent.state;
var target = window.location.href.split('#');
var href = target[0];
if (state) {
$('html,body').animate({ scrollTop: 0 }, 300, function() {
history.replaceState({}, null, href);
})
}
});
window.onload = function() {
history.replaceState({ path: window.location.href }, '');
}
});
最后,下面的版本有效:
$(window).bind("load", function () {
$('a[href*="#"]:not([href="#"])').click(function(event) {
event.preventDefault();
if (location.pathname.replace(/^'//,'') == this.pathname.replace(/^'//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
var hash = this.hash; // 'this' will not be in scope in callback
$('html,body').animate({ scrollTop: target.offset().top - 55}, 300, function() {
href = window.location.href;
history.pushState({page:href}, null, href.split('#')[0]+hash);
});
return false;
}
}
});
$(window).bind('popstate', function(event) {
var state = event.originalEvent.state;
var target = window.location.href.split('#');
var href = target[0];
if (state) {
$('html,body').animate({ scrollTop: 0 }, 300, function() {
history.replaceState({}, null, href);
});
}
});
});
似乎对于初始版本,脚本是在页面加载之前执行的,也许画布也没有加载。通过将所有脚本放入" $(window).bind("load",
,我认为一旦加载了所有脚本,就会执行转到锚点并返回页面顶部。
相关文章:
- 避免在用ng href加载样式表之前显示内容
- 希望在单击href链接时显示我的广告
- 将href链接添加到通过js鼠标悬停显示的图像
- CSS/JS:如何在悬停时显示文本,并显示点击href的文本框
- 如何确保在 HREF 触发之前下载并显示映像
- 如何将 Javascript 变量值显示到 HTML href 标签
- 单击 标记时,显示叠加层,如果选择“是”作为选项,则跟随 的 href URL
- 需要按钮显示/隐藏图像旋转 href 添加到 javascript 代码中
- 语法错误,无法识别的表达式,未显示 a[href 和画布
- 为什么 window.location.href 在 IE 中不显示哈希
- HTML:一种防止在左下角显示href的方法 谷歌浏览器.
- location.href将鼠标悬停在链接上时,使目标显示在状态栏中
- 'href'单击时不显示输出
- 使用href时会显示图像
- jquery 相对于 href=“#” 删除显示块
- 显示文本中的x个字符,当单击href时,用换行符显示其余字符
- 突出显示菜单中具有href="#"“选项”;a“;标签
- 每次点击href显示相同的DIV使用jquery
- 如何避免值不与url一起显示,而使用window.location.href将值从一个页面传递到另一个
- 使用ID从锚获取href显示未定义