为什么警告AnchorElement()警告href属性?
A link $.each($('a'), function(index,value){ alert (value) });
它会提醒: https://stackoverflow.com/questions/11076627/why-alert-anchorelement-a-alerts-the-href-attribute/url
。 为什么会这样?
这是因为锚的toString()
给出了URL。
现场演示
alert
调用toString()
隐含在对象上。 所以当你警告一个数组时:
[1,2,3,4,5,6]
提醒它会给你:
"1,2,3,4,5,6"
因为array
的toString()是用逗号分隔的元素。
如果在调试时遇到此问题,则应使用console.log()
而不是alert()
value
是HTMLAnchorElement
对象, HTMLAnchorElement.toString()
方法返回其url属性。
alert
在显示对象时将使用.toString()
。
你得到url的原因是因为.each接受一个容器(数组||对象),然后使用你的回调函数来处理容器。 它将获取数组的元素或对象的属性,并迭代它们。 由于您的选择器对象的属性是“href”,您将获得url值。