JavaScript...无法调用未定义的方法匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13400982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript...Cannot call method match of undefined
提问by jared_flack
I'm getting this error: Uncaught TypeError: Cannot call method 'match' of undefined
in my JavaScript. I'm trying to write this without jQuery since it'll be the only js on the site.
我收到此错误:Uncaught TypeError: Cannot call method 'match' of undefined
在我的 JavaScript 中。我正在尝试在没有 jQuery 的情况下编写它,因为它将成为网站上唯一的 js。
My code is supposed to add a class, "active", to the <a href="...>
in the navigation that links to the current page.
我的代码应该<a href="...>
在链接到当前页面的导航中添加一个“活动”类。
I'm guessing it might be the contentLoaded
function?....source
我猜这可能是contentLoaded
功能?....来源
Here's my code...(error occurs on line 9)...fiddle
这是我的代码...(错误发生在第 9 行)...小提琴
(function(window, document, undefined) { contentLoaded(window, function() {
var page = document.location.pathname.match(/[A-z]+$/)[0],
nav_anchors = document.getElementsByTagName('header')[0]
.getElementsByTagName('nav')[0]
.getElementsByTagName('a');
for(i in nav_anchors)
if(nav_anchors[i].href.match(/[A-z]+$/)[0] = page) //LINE 9 <-- Error
nav_anchors[i].classList.add('active');
})
})(this, this.document)
Thanks!
谢谢!
回答by Quentin
NodeLists
have length
, item
and namedItem
properties.
NodeLists
有length
,item
和namedItem
属性。
Use for (var i = 0; i < foo.length; i++)
not for i in foo
to iterate over them and only hit the nodes.
使用for (var i = 0; i < foo.length; i++)
notfor i in foo
迭代它们,只命中节点。
回答by Salketer
(function(window, document, undefined) { contentLoaded(window, function() {
var page = document.location.pathname.match(/[A-z]+$/)[0],
nav_anchors = document.getElementsByTagName('header')[0]
.getElementsByTagName('nav')[0]
.getElementsByTagName('a');
for(var i=0;i<nav_anchors.length;i++)
if(typeof nav_anchors[i].href != "undefined" && nav_anchors[i].href.match(/[A-z]+$/)[0] == page)
nav_anchors[i].classList.add('active');
})
})(this, this.document)
Added a check if the href
of the anchor is set, in case you have page anchors without href attributes (Such as <a name="top">
called using #top). Added a second =
in the IF
for it to work as exepected. And corrected the for syntax.
添加了一个检查是否href
设置了锚点,以防您有没有 href 属性的页面锚点(例如<a name="top">
使用 #top 调用)。添加第二个=
在IF
为它工作作为exepected。并更正了 for 语法。
回答by metadings
if(nav_anchors[i].href.match(/[A-z]+$/)[0] = page) //LINE 9 <-- Error
In javaScript the equals sign =
is an assignment operator. You probably mean double equals ==
where you can check values without type coercion (5 == "5"
) or triple equals ===
for strong typed checks (where 5 !== "5"
).
在 JavaScript 中,等号=
是赋值运算符。您可能指的是双等号==
,您可以在没有类型强制的情况下检查值 ( 5 == "5"
) 或三重等号===
进行强类型检查 ( where 5 !== "5"
)。
回答by Mic
Sometimes document.location.pathname.match(/[A-z]+$/)
can be null. If you try to use document.location.pathname.match(/[A-z]+$/)[0]
you can not access the 0 Element of null.
有时document.location.pathname.match(/[A-z]+$/)
可以为空。如果尝试使用document.location.pathname.match(/[A-z]+$/)[0]
则无法访问 null 的 0 元素。