开发者

How to refer to currently selected element in jQuery?

I'm trying to make a script that would insert img element after each link to a certain site with source equal to value of href attribute of given link. This is what I came up with:

$("a[href*=site.com/img/]").after("<img src="+$开发者_开发知识库(this).attr("href")+">");

Problem is, $(this) doesn't work (attr() returns undefined). Why is it? How to refer to selected link and its argument href?


Expand it out manually with each(), eg:

$("a[href*=site.com/img/]").each (function () {
    $(this).after("<img src="+$(this).attr("href")+">");
});

You need to be in the right scope for $(this) to behave as you expect it to, jQuery needs some kind of context to set it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜