Jquery 调用onClick函数后停止href链接

Jquery 调用onClick函数后停止href链接,jquery,html,ajax,Jquery,Html,Ajax,我有一个链接: <h2><a href=?p=article&aid='.$article->id.'" onclick="loadPage('article'); return false;">.$article->title.</a></h2> 但在运行javascript之后,href仍然处于活动状态。 我用了return-false;在我的onClick上,为了防止这种情况发生,但这次它不起作用???您最好使用jQue

我有一个链接:

<h2><a href=?p=article&aid='.$article->id.'" onclick="loadPage('article'); return false;">.$article->title.</a></h2>
但在运行javascript之后,href仍然处于活动状态。
我用了return-false;在我的onClick上,为了防止这种情况发生,但这次它不起作用???

您最好使用jQuery将click事件绑定到您的函数,因为逻辑最好是分开的

$('h2 a').click(function(event) {
    $("#content").html("Loading...");

    $.post("page.php", {p: page}, function(data){
        $("#content").html(data);   
    });

    return false;
});
通常,如果
href
仍处于活动状态,则很可能存在JavaScript错误

编辑:您还可以使用以这种方式绑定的单击函数来停止
href


最后,
href
的格式确实很好(缺少开头的引语)。这是打字错误吗?

看起来您的脚本有错误,没有到达返回块。如果添加try-catch,它将忽略脚本中的错误

function loadPage(page){
try{
    $("#content").html("Loading...");

    $.post("page.php", {p: page}, function(data){
        $("#content").html(data);   
    });
} catch(err){}
}

我最喜欢的方法是替换href属性。这样做的好处是保留了与非JS客户端的兼容性,并且不必处理“停止”事件或任何类似的向导。例如:

function unobtrusiveLinkSetup(link) {
    // replace the about link's href
    J(link).attr('jshref', J(link).attr('href'));
    J(link).removeAttr('href');

    // handle the link's action
    J(link).click(function(index) {
            J(this).stop();
            // make a request in the background
            J.get(J(this).attr('jshref'), function(result) {
                // whatever you need to do with the link..
            });
    });
}

然后,您只需执行
不引人注目的链接设置(“#myLink”)在文档的就绪函数中,或其他任何位置

请检查并更新您的第一个代码片段(只需复制粘贴您实际拥有的代码片段),因为这一个代码片段根本无法执行任何操作。您在athesyn.phpI中执行什么样的处理a
e.preventDefault()
(其中
e
是传递给click处理程序的事件参数)更合适()这正是我所需要的。我正在从我的链接中删除onclick事件,但保留了href事件。谢谢
function unobtrusiveLinkSetup(link) {
    // replace the about link's href
    J(link).attr('jshref', J(link).attr('href'));
    J(link).removeAttr('href');

    // handle the link's action
    J(link).click(function(index) {
            J(this).stop();
            // make a request in the background
            J.get(J(this).attr('jshref'), function(result) {
                // whatever you need to do with the link..
            });
    });
}