Jquery重写url循环
仍在尝试学习 jquery 的基础知识,所以在周末我开始研究在greasemonkey 中简单重写链接。该脚本正在运行,但没有循环。 它只是从第一个 .img 中获取 url 并将其写入所有其他 .img 中,而不是获取每个链接并将其写入同一元素中。不确定这是否有意义,但看看脚本,我相信您会明白。 :)
function rewrite() {
$.each($(".img"),function(){
var a=$("a img[src*='/SAN/']").attr('src');
$("a img[src*='/SAN/']").parent().attr('href','http://somesite.com/'+a);
});
}
Still trying to learn the basic of jquery so in the weekend I started looking on simple rewriting of links in greasemonkey. The script is working but it's not looping.
It just take the url from the first .img and and write it into all the other .img instead of getting the each link and write it into the same element. Not sure if it made any sense but take a look at the script and I'm sure you understand. :)
function rewrite() {
$.each($(".img"),function(){
var a=$("a img[src*='/SAN/']").attr('src');
$("a img[src*='/SAN/']").parent().attr('href','http://somesite.com/'+a);
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来这就是您想要的:
循环遍历每个图像并根据
src
href
属性> 您正在循环的当前图像,在.each()
循环是这里的关键。否则.attr()
从 first< /em> 它匹配的元素,而不是您正在循环的当前元素。It looks like this is what you're after:
This loops through each image and sets the parent
<a>
href
property based on thesrc
of the current image you're looping over, usingthis
inside that.each()
loop is the key here. Otherwise.attr()
gets the attribute from the first element it matches, rather than the current element you're looping over.