简体   繁体   English

通过ajax加载PHP后,jQuery无法正常工作

[英]Jquery doesn't work after loading a PHP by ajax

I try to load a php file by ajax. 我尝试通过ajax加载php文件。

This works fine by this code: 通过以下代码可以正常工作:

<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>

But in my php file are jquery plugins, which dont work after loading via ajax... Maybe the solution is to using the ajax by jquery syntax. 但是在我的php文件中是jquery插件,通过ajax加载后无法工作...也许解决方案是通过jquery语法使用ajax。 Is it right? 这样对吗?

I tried it, but my Ajax didn't load the php... It should load the php automaticlly by loading the page in a defined div. 我试过了,但是我的Ajax没有加载php ...它应该通过在定义的div中加载页面来自动加载php。

Many thanks in advance! 提前谢谢了!

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

use Jquery ajax in stead. 改为使用Jquery ajax。 for example: 例如:

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

A small part a solved 一小部分解决了

a small script in myfile.php works now: 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>

Source of this solution: JQuery effect doesnt work for ajax content 该解决方案的来源: jQuery效果不适用于ajax内容

But this huge script is stil not working: 但是这个巨大的脚本仍然无法正常工作:

<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>

**The Jquery Scripts are in my index.php not in myfile.php. ** Jquery脚本不在我的index.php中,不在myfile.php中。 only the html markup an in the myfile.php myfile.php中只有html标记

Okay, I found an answer by myself. 好吧,我自己找到了答案。

Im using this: 我正在使用这个:

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

And pasting my scripts in the succes function. 并将我的脚本粘贴到成功函数中。 But not everything is working. 但是,并非一切都正常。 I'm on it. 我在上面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM