简体   繁体   English

Jquery:还有另一种写法吗,也许使用.attr?

[英]Jquery: is there another way to write this, maybe using .attr?

I thought I could re-write the line below:我想我可以重写下面的行:

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

to something like this:像这样:

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

obviously with var inview = yada yada yada defined above it.显然,上面定义了var inview = yada yada yada

or is there another way to write the first line up there,还是有另一种方法可以在那里写第一行,

using jquery and getting rid of the [hash=' for something like.attr or.something(inview)?使用 jquery 并摆脱像.attr 或.something(inview) 这样的[hash='

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

That should do it.那应该这样做。

You want something like:你想要这样的东西:

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

$('a').attr('hash', inview); , which is what I assume you meant for your second line, actually sets the hash attribute to inview. ,这就是我假设您对第二行的意思,实际上hash 属性设置为 inview。

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

For reference that would set the href of all a elements to inview作为参考,将所有 a 元素的 href 设置为 inview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM