Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使脚本在每次使用ajax从WebMethod加载数据时运行?_Javascript_Jquery_Asp.net_Ajax_Webmethod - Fatal编程技术网

Javascript 如何使脚本在每次使用ajax从WebMethod加载数据时运行?

Javascript 如何使脚本在每次使用ajax从WebMethod加载数据时运行?,javascript,jquery,asp.net,ajax,webmethod,Javascript,Jquery,Asp.net,Ajax,Webmethod,我有一个问题,页面加载时javascript脚本只加载一次,但每次从服务器端的WebMethod获取数据时,我都需要它 这是我的密码: 我的JavaSript ajax: <script type="text/javascript"> $(document).ready(function () { function InfiniteScroll() { $('#divPostsLoader').html('<img src

我有一个问题,页面加载时javascript脚本只加载一次,但每次从服务器端的WebMethod获取数据时,我都需要它

这是我的密码:

我的JavaSript ajax:

<script type="text/javascript">


    $(document).ready(function () {

        function InfiniteScroll() {

            $('#divPostsLoader').html('<img src="img/loader.gif">');

            $.ajax({
                type: "POST",
                url: "Home.aspx/GetLastArticles",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (data != "") {

                        $('#divPostsLoader:last').after(data.d);

                    }
                    $('#divPostsLoader').empty();
                },
                error: function (data) {
                    if (!document.getElementById('#divPostsLoader').innerHTML == "<p>END OF POSTS</p>")
                        $('#divPostsLoader').empty();
                    $('#divPostsLoader').html("<p>END OF POSTS</p>");
                }
            })

        };
        var b = false;
        //When scroll down, the scroller is at the bottom with the function below and fire the lastPostFunc function
        $(window).scroll(function () {

            if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
                InfiniteScroll();

            }

            b = true;
        });
        if (!b) {

            window.onload = function () {
                InfiniteScroll();

            };

        }
    });

</script>
我的aspx页面(客户端):


在ajax成功中调用函数

success: function (data) {
        if (data != "") {
                $('#divPostsLoader:last').after(data.d);
       }
        $('#divPostsLoader').empty();
        $(".tag_button").click();
},

更改为以下内容,eventhandler也会附加到动态添加的元素

$(document).on('click', '.tag_button', function() {...});
语法解释

只是为了澄清代码。语法如下:

$(document).on(events, cssSelector, handler);

请参阅全文。通过选择
document
我们确保现在和将来都将此事件处理程序加载到文档中的所有元素。

谢谢,这对我帮助很大
$(document).on('click', '.tag_button', function() {...});
$(document).on(events, cssSelector, handler);