【问题标题】:Why does my jquery $.get() not display any response为什么我的 jquery $.get() 不显示任何响应
【发布时间】:2009-02-24 06:50:10
【问题描述】:

这是我的 shop.html sn-p:

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

这是我的 main.js sn-p:

$("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 服务器中运行。 想知道为什么我的 $.get() 函数什么也没发生。

帮帮我..请... T.T

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您不会取消点击事件对点击链接的默认操作,因此当您点击链接时,您只需在 $.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();
    });
    

    【讨论】:

    • 谢谢。这行得通。出现警报。但响应没有出现在 id=paneMiddle 的 div 中。 :(
    • 如果你用 $('div#paneMiddle') 来引用它会起作用吗?
    【解决方案2】:

    尝试使用 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】:

      试试这个

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

      });

      【讨论】:

        【解决方案4】:

        不使用 标签作为你的点击目标怎么样 .. 也许只使用 span 代替?

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

        然后简单

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-12
          • 1970-01-01
          相关资源
          最近更新 更多