JQuery:带有属性过滤器的选择器链:结果不持久?
我要拔头发了!! grrr...
这似乎可以工作:
//get all the foo and bar links that point to a named anchor:
$("a.foo,a.bar").filter("[href^=#]").click
(
function()
{
doSomething( $(this).attr("href").substr(1) );
return false;
}
);
当我将 '$(this).attr("href").substr(1)' 的输出记录到控制台时,我看到没有哈希值的锚点名称,所有相关链接。一切都好。但是在 doSomething() 里面, typeof() 说我有一个字符串 - 但它是空的!为什么?!!
function doSomething(str)
{
//log str : it's empty!!
}
通常,在我在这里发布问题后不久,任何人都无法帮助我,这太奇怪了,或者我稍后会发现自己的愚蠢。我希望这次有人能帮忙! :-(
============== 已解决 =====================
**** ***** 这是我自己的愚蠢 **********
在 doSomething 中,我的代码做的第一件事是:(
if(str = "") return;
拍拍额头)谢谢大家让我以不同的方式盯着这个。
I am pulling my hair out!! grrr...
This seems to work:
//get all the foo and bar links that point to a named anchor:
$("a.foo,a.bar").filter("[href^=#]").click
(
function()
{
doSomething( $(this).attr("href").substr(1) );
return false;
}
);
When I log the output of '$(this).attr("href").substr(1)' to the console I see the anchorname without the hash, for all the relevant links. All good. But inside doSomething(), typeof() of says I have a string - but it's empty!! Why?!!
function doSomething(str)
{
//log str : it's empty!!
}
Usually soon after I post a question on here it's too bizarre for anyone to help me or I figure out my own stupidity later. I hope someone can help this time! :-(
============== SOLVED =====================
********* It was my own stupidity **********
inside doSomething the first thing that my code did was:
if(str = "") return;
(slaps forehead). Thank you all for making me stare at this in a different way
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法重现此问题。 听起来像是
doSomething
代码本身的问题 -进行快速代码审查,以确保您指向正确的函数并且在使用之前没有修改参数。I can't reproduce this. Sounds like an issue with the
doSomething
code itself - do a quick code review to make sure you're pointing to the right function and that you're not modifying the argument before use.注意:这不是正确答案!但我将把它留在这里,以防其他 Sizzle 用户有用。
这是 Sizzle(jQuery 使用的选择器库)中的一个错误。
问题是,当您在 JavaScript 中访问
link.href
时,您不会获得href
属性的文本,而是获得已解析的绝对 URL。当然,这以http://
开头,而不是#
。当您调用
link.attr('href')
时,jQuery 会检测到特殊情况并应用解决方法。当您使用选择器时,Sizzle 不会。它尝试过:但此解决方法在 IE 版本 7 之前不起作用,因为它们使
getAttribute
等于仅获取 JavaScript 属性。 [正确的解决方法是使用getAttributeNode
代替,或者嗅探 IE 并使用专有的getAttribute('href', 2)
扩展。]因此选择器引擎获取
http://...
作为 href 属性,当然这永远不会匹配^=#
。如果您自己使用过滤功能进行操作,它应该可以工作:
note: This isn't the correct answer! But I'm going to leave it here in case it is of use to other Sizzle users.
It's a bug in Sizzle (the selector library used by jQuery).
The problem is when you access
link.href
in JavaScript you don't get the text of thehref
attribute, you get the resolved absolute URL. Naturally this begins withhttp://
and not#
.When you call
link.attr('href')
jQuery detects the special case and applies a workaround. When you use a selector, Sizzle doesn't. It tries:but this workaround doesn't work on IE up to version 7, as they make
getAttribute
equal to just getting the JavaScript property. [The correct workaround would be to usegetAttributeNode
instead, or sniff IE and use the proprietarygetAttribute('href', 2)
extension.]So the selector engine gets
http://...
for the href attribute and of course this never matches^=#
.If you do it yourself with a filter function it should work:
尝试
substr(0)
第一个字符位于索引 0 http:// www.w3schools.com/jsref/jsref_substr.asptry
substr(0)
first character is at index 0 http://www.w3schools.com/jsref/jsref_substr.asp