如何从同一页面上的ajax加载内容访问javascript函数?

时间:2022-10-06 22:53:02

1) on main page i have javascript function

1)在主页面上我有javascript功能

function editSuccess(data) { alert("editSuccess called"); }

function editSuccess(data){alert(“editSuccess called”); }

2) From main page I load some content into div using ajax then i need call javascript function from loaded content but it's don't see editSuccess function

2)从主页面我使用ajax将一些内容加载到div然后我需要从加载的内容调用javascript函数但是它没有看到editSuccess函数

1 个解决方案

#1


0  

This should work as long as editSuccess is available in the global scope. Remember that JavaScript has function scope, so if you define editSuccess inside a function, it will only be available within that function. Consider this example:

只要editSuccess在全局范围内可用,这应该有效。请记住JavaScript具有函数作用域,因此如果在函数内定义editSuccess,它将仅在该函数中可用。考虑这个例子:

// javascript
window.onload = function() {
    function editSuccess() {
        alert("Hello");
    }
};

// html
<div onclick='editSuccess()'>..</div>

// or
<script>editSuccess()</script>

will not work since editSuccess does not exist in the global scope.

由于editSuccess在全局范围内不存在,因此无法正常工作。

#1


0  

This should work as long as editSuccess is available in the global scope. Remember that JavaScript has function scope, so if you define editSuccess inside a function, it will only be available within that function. Consider this example:

只要editSuccess在全局范围内可用,这应该有效。请记住JavaScript具有函数作用域,因此如果在函数内定义editSuccess,它将仅在该函数中可用。考虑这个例子:

// javascript
window.onload = function() {
    function editSuccess() {
        alert("Hello");
    }
};

// html
<div onclick='editSuccess()'>..</div>

// or
<script>editSuccess()</script>

will not work since editSuccess does not exist in the global scope.

由于editSuccess在全局范围内不存在,因此无法正常工作。