为什么我的jquery $ .get()没有显示任何响应

时间:2009-02-24 06:50:11

标签: jquery

这是我的shop.html代码段:

<li><a id="linkShop" href="shop.html">shop</a></li>
<div id="paneMiddle"></div>

这是我的main.js片段:

$("a[id=linkShop]").click(function(){
    $.get("data1.html", function(response){
        $("div[id=paneMiddle]").html(response);
        alert("hi"+ response);
    }); 

});

data1.html包含以下内容:

<p>data 1 - test</p>

我真的不知道为什么会发生这种情况,我之前在php中尝试过$ .get, 但现在,我正在尝试使用jquery w / jsp,在tomcat服务器上运行。 idk为什么我的$ .get()函数没有任何结果。

帮助我..Pls ... T.T

4 个答案:

答案 0 :(得分:4)

您没有取消单击链接上的click事件的默认操作,因此当您单击该链接时,您只需在$ .get获得任何响应之前重新加载页面。

尝试

$("a[id=linkShop]").click(function(){
        $.get("data1.html", function(response){
                $("div[id=paneMiddle]").html(response);
                alert("hi"+ response);
        });     
        return false;
});

$("a[id=linkShop]").click(function(evt){
        $.get("data1.html", function(response){
                $("div[id=paneMiddle]").html(response);
                alert("hi"+ response);
        });     
        evt.preventDefault();
});

答案 1 :(得分:0)

如何不使用&lt; A&gt;标记为您的点击目标..也许只是使用跨度?

<li><span id='linkShop' style='cursor:pointer'>shop</span></li>
<div id="paneMiddle"></div>

然后简单地

$("#linkShop").click(function() {
    $("#paneMiddle").load("data.html");
});

答案 2 :(得分:0)

尝试使用event.preventDefault取消链接的默认操作:

$("a[id=linkShop]").click(function(event){ 
        event.preventDefault(); 
        $.get("data1.html", function(response){ 

                $("div[id=paneMiddle]").html(response); 
                alert("hi"+ response); 
        });      
}); 

您可以对您的脚本here进行实时测试,并且可以修改加载了ajax的内容here

答案 3 :(得分:0)

试试这个

$("a#linkShop").click(function(){
    $.get("data1.html", function(response){
            $("div#paneMiddle").html(response);
            alert("hi"+ response);
    });     

});

相关问题