【问题标题】:Jquery: is there another way to write this, maybe using .attr?Jquery:还有另一种写法,也许使用.attr?
【发布时间】:2011-08-14 09:17:21
【问题描述】:

我想我可以重写下面的行:

$('a').filter('[hash=' + inview + ']');

到这样的事情:

$('a').attr(href, inview);

显然上面定义了var inview = yada yada yada

或者还有其他方法可以写第一行,

使用 jquery 并为 .attr 或 .something(inview) 删除 [hash='

【问题讨论】:

  • jQuery 的 attr() 函数获取和设置 HTML 属性和 DOM 属性的组合,具体取决于它认为您可能需要的属性。

标签: javascript jquery hash href


【解决方案1】:
$('a[hash=' + inview + ']');

应该可以的。

【讨论】:

  • 但是有没有其他方法可以做到这一点,而不使用[hash=
【解决方案2】:

你想要这样的东西:

$('a[hash="' + inview + '"]')

$('a').attr('hash', inview);,这就是我假设您对第二行的意思,实际上 设置 哈希属性为 inview。

【讨论】:

    【解决方案3】:
    $('a').attr(href, inview);
    

    将所有 a 元素的 href 设置为 inview 的参考

    【讨论】:

    • 很好,我以为我们只是假设。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    相关资源
    最近更新 更多