jquery .load()不会变量

时间:2011-04-19 16:50:38

标签: javascript jquery

我有这个代码

$(document).ready(function () {
    $('a').click(function(e) {
        e.preventDefault();
        url = $(this).attr('href');
        $("body").load(href);
    });
});

但它不起作用。什么都没有加载。唯一有效的是e.preventdefault()

我做错了吗?

3 个答案:

答案 0 :(得分:6)

$("body").load(href);

应该是:

$("body").load(url);

或者你可以完全取消变量:

$("body").load(this.href);

答案 1 :(得分:0)

您错过了(this).attr('href');之前的 $ ,并且还使用值url进行加载函数调用。

$(document).ready(function () {
    $('a').click(function(e) {
            e.preventDefault();
            url = $(this).attr('href');
            $("body").load(url);
    });
});

答案 2 :(得分:0)

您已将href attr分配给名为url的变量,然后尝试将href用作变量。

不好:

url = $(this).attr('href');    
$("body").load(href);

好:

url = $(this).attr('href');    
$("body").load(url);