【问题标题】:Jquery doesn't work after loading a PHP by ajax通过ajax加载PHP后Jquery不起作用
【发布时间】:2016-01-20 12:19:58
【问题描述】:

我尝试通过 ajax 加载一个 php 文件。

这段代码可以正常工作:

<body id="top">     
<div id="loadajaxhere"></div>
<script>
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("loadajaxhere").innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET", "myfile.php", true);
    xmlhttp.send();
</script>

但在我的 php 文件中是 jquery 插件,通过 ajax 加载后无法工作... 也许解决方案是通过 jquery 语法使用 ajax。对吗?

我试过了,但是我的 Ajax 没有加载 php... 它应该通过在定义的 div 中加载页面来自动加载 php。

提前非常感谢!

【问题讨论】:

  • myfile.php的内容是什么?
  • 有没有JQuery函数调用?请放置该代码。还要检查您的浏览器控制台是否有 JS 错误(如果有)。
  • 无需使用 ajax 加载任何 javascript。只需将其添加到当前页面,如有必要,在某些逻辑中。例如stackoverflow.com/questions/15521343/…
  • 我通过复制 myfile.php 的内容而不是
    来尝试内容。这很好用。内容是普通的html、php和jquery脚本,但这不可能是问题。

标签: javascript php jquery html ajax


【解决方案1】:

改用 Jquery ajax。例如:

$.ajax({
        url: 'myfile.php',
        type: 'GET',
        data: {'submit':'true'}, // An object with the key 'submit' and  value'true';
        success: function (result) {
          document.getElementById("loadajaxhere").innerHTML = result;
        }
    }); 

【讨论】:

  • 它正在加载,但我的内容文件中的 jquery 脚本不起作用。
【解决方案2】:

解决了一小部分

myfile.php 中的一个小脚本现在可以工作了:

<script type="text/javascript">
//$('.triggermore').click(function(){ //<- This is the old line
$('body').on('click', '.triggermore', function(event){ //<- This is the new one
        $(".weitere").slideDown();
        $(".weitere").addClass("open");
        $(".triggermore").addClass("bye");
        $('html, body').animate({ scrollTop: $("#weitere").offset().top }, 1000);       
});
</script>

本解决方案来源:JQuery effect doesnt work for ajax content

但是这个巨大的脚本仍然无法运行:

<script type="text/javascript">
$(document).ready(function() {

    //http://webdesign.tutsplus.com/tutorials/javascript-tutorials/create-a-sticky-navigation-header-using-jquery-waypoints/
    var nav_container = $(".menu");
    var nav = $("nav");

    var top_spacing = 0;
    var waypoint_offset = '40%';
    var first_section = '0%';

    nav_container.waypoint({
        handler: function(event, direction) {

            if (direction == 'down') {
                nav_container.addClass("sticky")
                .stop()
                .css("top", -nav.outerHeight())
                .animate({"top" : top_spacing});
            } else {    
                var inputPos = $( 'input:first' ).position();
                nav_container.stop().removeClass("sticky").css("top",first_section).animate({"top":first_section});
            }

        },
        offset: function() {
            return -nav.outerHeight()-waypoint_offset;
        }   

    });

    var sections = $("section");
    var navigation_links = $(".menu li a");
    var links = $("nav a");

    sections.waypoint({
        handler: function(event, direction) {

            var active_section;
            active_section = $(this);
            if (direction === "up") active_section = active_section.prev();

            var active_link = $('.menu li a[href="#' + active_section.attr("class") + '"]');
            navigation_links.removeClass("selected");
            if(active_section.attr("class") != "top") {
                active_link.addClass("selected");
            }

        },
        offset: waypoint_offset
    })


    links.click( function(event) {

        $.scrollTo(
            $(this).attr("href"),
            {
                duration: 1500,
                offset: { 'left':0, 'top':0 }
            }
        );
    });




});

</script>

**Jquery 脚本在我的 index.php 中,而不是在 myfile.php 中。 只有 myfile.php 中的 html 标记

【讨论】:

    【解决方案3】:

    好的,我自己找到了答案。

    我正在使用这个:

    $.ajax({
            url: 'myfile.php',
            type: 'GET',
            data: {'submit':'true'}, // An object with the key 'submit' and  value'true';
            success: function (result) {
              document.getElementById("loadajaxhere").innerHTML = result;
            }
        }); 
    

    并将我的脚本粘贴到 succes 函数中。但并非一切正常。 我在做。

    【讨论】:

      【解决方案4】:
        <script>
             $.ajax({
                      url: "myfile.php", 
                      success: function(result){
                           $("#loadajaxhere").html(result);
                      }
            });
       </script>
      

      【讨论】:

      • 好的,它正在加载,但它返回的结果与非 jquery 函数不同。
      • 请解释你的答案。它目前被视为低质量的帖子。谢谢!
      猜你喜欢
      • 2017-01-14
      • 2011-10-13
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多