I'我在$('a[href*=#]').each(function)处得到非法字符错误.可能的原
I'm getting the illegal character error at $('a[href*=#]').each(function(). what could be the possible reasons?
jquery文件也包含在内
它使用的引号有错误吗?什么可以替换?
<!----- JQUERY FOR SLIDING NAVIGATION --->
<script>
$(document).ready(function() {
$('a[href*=#]').each(function() {
if (location.pathname.replace(/^'//, '') == this.pathname.replace(/^'//, '')
&& location.hostname == this.hostname
&& this.hash.replace(/#/, '')) {
var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) + ']');
var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
if ($target) {
var targetOffset = $target.offset().top;
}
}
});
});
</script>
由于#
是元字符,因此将#
作为string
传递,或者只尝试将其作为escape
。
试试,
$('a[href*="#"]').each(function() {
或
$('a[href*=''#]').each(function() {
请阅读此处了解更多有关元字符的信息。
名称属性中有特殊字符。因此用引号括起来:
$('a[href*="#"]').each(function() {
//rest code
});
如果我可以建议一种普通的方法。
document.links
已经返回页面中的所有链接。Array.filter
过滤数组,但它是泛型的。
你可以通过如下散列来过滤链接:
var filter = Array.prototype.filter;
var withHash = filter.call(document.links,function(a){ return a.hash; });
// withHash now contains all links with a # hash
withHash.forEach(function(el){
console.log(el," is an anchor element");
});
考虑到您已经在代码中使用了.hash,这似乎更自然。它也不需要jQuery工作,无论有没有它都可以工作。
相关文章:
- "非法字符“;Watir execute_script时出错
- Firefox控制台获取'SyntaxError:非法字符
- 检查任何输入或文本区域中是否存在非法字符
- 为什么我得到“;非法字符语法错误未终止的字符串文字“;当我使用javascript时
- Worklight构建将非法字符插入到Javascript文件中
- jQuery 中的非法字符错误 - 无论函数内容如何
- 路径错误中的非法字符
- JavaScript正则表达式给出非法字符的错误
- 尝试添加 jQuery 和引导程序时出现 jQuery 非法字符
- 使用闭包编译器时出现非法字符错误.jar使用 ANT 进行 r.js 优化
- 为什么我收到错误“语法错误:非法字符”
- JSON 响应中的非法字符
- JSON.分析未处理非法字符
- Javascript非法字符错误
- Javascript文件中的非法字符导致表单无法使用Jquery
- javascript文件中附加了非法字符
- 从DOM ID中删除非法字符的Javascript正则表达式
- 在试图将事件记录到flush中时,javascript中出现了非法字符语法错误
- ASP.NET:非法字符'@'在'@Html.Raw'呼叫
- 将十六进制颜色传递到函数时,firefox中存在非法字符