加载函数在满足 jQuery 中的 if 条件后不起作用

Load function is not working after satisfying if condition in jQuery

本文关键字:if 条件 不起作用 中的 jQuery 函数 满足 加载      更新时间:2023-09-26

在下面的代码中,if 条件正在工作,因为我已经检查了警报,但加载函数不起作用。

我的代码:

$("a").click(function() {                            
    hreff = $(this).attr('href');
    if(hreff == "something" )
    {
        $("#dump").load("someurl");
    }
});
$("a").click(function(e) {
    e.preventDefault();  // prevent page reload
    var hreff = $(this).attr('href');
    if (hreff == "something") {
        $("#dump").load("someurl");
    }
});

函数加载将从定义的 URL 加载响应,但您尚未定义 URL。如果 $(this).attr('href') 有一个真实的 URL,那么你将永远不会得到它们,因为你在 if 中签入了"某物"

$("a").click(function() {
    href = $(this).attr('href');
    if (href.length >0) {
        $("#dump").load(href, function() {
            alert('Load was performed.');
        });
    }
});

这将起作用。