JQuery:带有属性过滤器的选择器链:结果不持久?

发布于 2024-08-18 06:27:07 字数 804 浏览 4 评论 0原文

我要拔头发了!! 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳哥哥 2024-08-25 06:27:07

我无法重现此问题。 听起来像是 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.

月野兔 2024-08-25 06:27:07

注意:这不是正确答案!但我将把它留在这里,以防其他 Sizzle 用户有用。


这是 Sizzle(jQuery 使用的选择器库)中的一个错误。

问题是,当您在 JavaScript 中访问 link.href 时,您不会获得 href 属性的文本,而是获得已解析的绝对 URL。当然,这以 http:// 开头,而不是 #

当您调用 link.attr('href') 时,jQuery 会检测到特殊情况并应用解决方法。当您使用选择器时,Sizzle 不会。它尝试过:

attrHandle: {
    href: function(elem){
        return elem.getAttribute("href");
    }
},

但此解决方法在 IE 版本 7 之前不起作用,因为它们使 getAttribute 等于仅获取 JavaScript 属性。 [正确的解决方法是使用 getAttributeNode 代替,或者嗅探 IE 并使用专有的 getAttribute('href', 2) 扩展。]

因此选择器引擎获取 http://... 作为 href 属性,当然这永远不会匹配 ^=#

如果您自己使用过滤功能进行操作,它应该可以工作:

$('a.foo, a.bar').filter(function() {
    return $(this).attr('href').substring(0, 1)==='#';
})...

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 the href attribute, you get the resolved absolute URL. Naturally this begins with http:// 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:

attrHandle: {
    href: function(elem){
        return elem.getAttribute("href");
    }
},

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 use getAttributeNode instead, or sniff IE and use the proprietary getAttribute('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:

$('a.foo, a.bar').filter(function() {
    return $(this).attr('href').substring(0, 1)==='#';
})...
不打扰别人 2024-08-25 06:27:07

尝试 substr(0) 第一个字符位于索引 0 http:// www.w3schools.com/jsref/jsref_substr.asp

try substr(0) first character is at index 0 http://www.w3schools.com/jsref/jsref_substr.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文