attr未定义

Mat*_*att 2 javascript jquery attr

以下代码不起作用,因为attr未定义:

$("#foo a[href]").each(function()
{
    this.attr("href", "www.google.com");
});
Run Code Online (Sandbox Code Playgroud)

但是这段代码确实:

$("#foo a[href]").each(function()
{
    this.href = "www.google.com";
});
Run Code Online (Sandbox Code Playgroud)

为什么??

Jar*_*eer 10

你需要包裹this......$(this)

attr是jQuery对象的方法,href是元素节点的属性