简体   繁体   English

调用html内的javascript页面函数

[英]call javascript page function inside ajaxed html

I have a page where i use jQuery to load some content into a div element 我有一个页面,在这里我使用jQuery将一些内容加载到div元素中

<div id="contents-box-in"></div>

jQuery code in page 页面中的jQuery代码

$(document).ready(function() {

        $("#contents-box-in").load("new-01.php", function() {               
            $('#contents-box-in').fadeIn(120);              
        });

        var updateBoxData = function(data) {

            $('#contents-box-in').fadeOut(100, function() {
                $('#contents-box-in').html(data).fadeIn(130);
            });

        }

    });

the content that i load is a form that needs to load a new page sending collected data from form 我加载的内容是需要加载一个新页面的表单,该页面从表单发送收集的数据

            $('#form-buttons-next').click(function(e) {

                var formData = new FormData($(this)[0]);

                var formS = $.ajax({
                    url         : 'new-02.php',
                    type        : 'POST',                   
                    data        : formData,
                    async       : false,
                    cache       : false,
                    processData : false,
                    contentType : false
                });

                formS.done(function(data) {

                   if (data != null) {

                        updateBoxData(data);

                    }

                });

                formS.fail(function(jqXHR, textStatus) {

                    alert("error");

                });

            });

since i do this in different step i would like to use a shared function contained in page that is loading the ajax content but i get updateBoxData is undefined 因为我在不同的步骤中执行此操作,所以我想使用页面中包含的共享功能来加载ajax内容,但我得到的updateBoxData是未定义的

I guess that ajaxed content can't see parent container function 我猜想ajax的内容看不到父容器的功能

The easy way would be to load a different .js file containing shared function, i was wondering if is possible to access the updateBoxData from ajaxed contents 最简单的方法是加载包含共享功能的其他.js文件,我想知道是否可以从ajaxed内容访问updateBoxData

...i would like to use a shared function contained in page that is loading the ajax content but i get updateBoxData is undefined ...我想使用加载ajax内容的页面中包含的共享功能,但我得到updateBoxData未定义

I guess that ajaxed content can't see parent container function 我猜想ajax的内容看不到父容器的功能

No, that's not why. 不,那不是原因。 Your updateBoxData variable is scoped to the function it's declared in (your ready ) callback. 您的updateBoxData变量的作用域范围是在(您ready )回调中声明的函数。 If you want it accessible globally, you'll need to make it global instead. 如果要使其全局访问,则需要使其全局。

The problem is, though, the global namespace is already incredibly crowded . 但是问题是,全局名称空间已经非常拥挤 So if you put all of your useful functions there as globals, you're going to run into conflicts sooner or later. 因此,如果将所有有用的功能都放在全局变量中,那么迟早会遇到冲突。

For that reason, for now until browsers support ES2015 modules (which will take a while), I suggest giving yourself just one global symbol, something unlikely to conflict with other things, and assigning an object to it with properties for your various utility functions. 因此,在浏览器支持ES2015模块之前(这将需要一段时间),我建议给自己一个全局符号(不太可能与其他符号冲突),并为它分配具有各种实用功能的属性的对象。 Eg: 例如:

var MyNiftyUtils = {
    updateBoxData: function() {
        // ...
    }
};

Then you call that via MyNiftyUtils.updateBoxData . 然后,通过MyNiftyUtils.updateBoxData调用。 If the verbosity bothers you, no worries, just use a scoping function and assign it to a local: 如果冗长困扰您,不用担心,只需使用作用域确定功能并将其分配给本地:

(function() {
    var u = MyNiftyUtils;

    // ....
    u.updateBoxData(/*...*/);
})();

(There are about 18 variations on that theme, this is just one of them.) (该主题大约有18种变体,这只是其中之一。)

The function updateBoxData is defined inside a callback function you passed to .ready and hence its scope is limited to that function. 函数updateBoxData是在传递给.ready的回调函数中定义的,因此其范围仅限于该函数。 Let us call this callback function Fx. 让我们将此回调函数称为Fx。

The click handler (the function passed to .click in the second part), which we call it Fy is defined outside of Fx and as a result does not have access to the variables defined in Fx (remember updateBoxData is a variable inside Fx). 点击处理程序(在第二部分中传递给.click的函数)在Fx之外定义,因此我们无法访问Fx中定义的变量(请记住updateBoxData是Fx内的变量)。

That is why your code does not work. 这就是为什么您的代码不起作用的原因。 To get it working simply take updateBoxData out of the callback in .ready function: 为了使它工作,只需将updateBoxData.ready函数的回调中updateBoxData

$(document).ready(function() {
    $("#contents-box-in").load("new-01.php", function() {               
        $('#contents-box-in').fadeIn(120);              
    });
});
function updateBoxData(data) {
    $('#contents-box-in').fadeOut(100, function() {
        $('#contents-box-in').html(data).fadeIn(130);
    });
}
...

The rest is the same. 其余部分相同。

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

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