dsl2014 2012-07-25 15:11
浏览 51
已采纳

将PHP脚本的结果从与JQuery的链接加载到div中

I have been trying to load the content of a link to a div element without reloading the page. I found this solution 'http://wecodesign.com/demos/stackoverflow-7071545.htm' and have tried it but I couldn't get it to work.

What I am doing is exactly the same, but for some reason it doesn't work.

This is the Jquery part:

<script type="text/javascript">

    function updateContainer( url ){
        dynamicCon = '#mainContent';    
        ObjTag = $( dynamicCon );
        ObjTag.load( url );
    }
    $( document ).ready( function() {
        $( 'a[rel="dynamicLoad"]' ).bind( "click", function( event ) {
            url = $( this ).attr( 'href' );
            updateContainer( url );
            event.preventDefault();
            event.stopPropagation();
        });
    });
</script>

And this is the PHP bit I use to output the HTML:

echo '<li><a href = "/getPost.php?q='. $row['id'] .' " rel="dynamicLoad" >'. $row['title'] .'</a> </li> <br>';

But this sends me to the link instead of loading it into the div. The div I want the content to load into is called "mainContent". Now, I can't say I know JQuery but everything looks OK to me but obviously I am missing something.

UPDATE: I tried linking to a later library, restart the server, cleared the browser cache and changed the Javascript to:

        function updateContainer( url ){
            dynamicCon = '#mainContent';    
            ObjTag = $( dynamicCon );
            $("#mainContent").load(url)
        }
        $( document ).ready( function() {
            $( 'a[rel="dynamicLoad"]' ).bind( "click", function( event ) {
                url = $( this ).attr( 'href' );
                updateContainer( url );
                event.preventDefault();
                event.stopPropagation();
            });
        });

Now, when I try to load a link the mainContent div becomes empty (certainly an improvement considering the situation)

UPDATE 2: I uploaded the thing on the net if you want to poke around. electricbrownie.net

  • 写回答

3条回答 默认 最新

  • dongxieli3839 2012-07-25 18:29
    关注

    Well the answer is simple. In your generated urls remove trailing spaces and it will work :)

    echo '<li><a href = "/getPost.php?q='. $row['id'] .'" rel="dynamicLoad" >'. $row['title'] .'</a> </li> <br>';
    

    it was <a href = "/getPost.php?q='. $row['id'] .' " should be <a href = "/getPost.php?q='. $row['id'] .'"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程