Jquery 在通过 ajax 加载 PHP 后不起作用

Jquery doesn't work after loading a PHP by ajax

本文关键字:PHP 不起作用 加载 ajax Jquery      更新时间:2023-09-26

我尝试通过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。

提前非常感谢!

使用 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;
        }
    }); 

一小部分解决了

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 效果不适用于 ajax 内容

但是这个庞大的脚本不起作用:

<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 脚本在我的索引中.php而不是在 myfile.php 中。只有 HTML 标记在 myfile 中.php

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

我使用这个:

$.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;
        }
    }); 

并将我的脚本粘贴到成功函数中。但并非一切都有效。我在上面。

  <script>
       $.ajax({
                url: "myfile.php", 
                success: function(result){
                     $("#loadajaxhere").html(result);
                }
      });
 </script>