jQuery的AJAX调用后执行脚本脚本、jQuery、AJAX

2023-09-10 14:28:52 作者:無法忘卻的溫柔

我加载某些HTML code与.load功能自动对焦jQuery的一个div包含一些code我想执行;

I load some HTML code into a div with the .load function af jQuery that contains some code I want to execute;

$.ajax({
    type: "GET",
    url: url,
    dataType: "html",
    success: function(data){
        //.....
   }
 });

加载Ajax请求的警报后();功能的工作原理,但在someFunction不会。

After loading the ajax request the alert(); function works, but the the someFunction won't.

$(document).ready(function(){
    alert('TEST'); //this works
    someFunction("#debug",'var');//this doesn't
});

我如何从一个Ajax调用执行此功能

How can I execute this function from an Ajax call

一个为&LT执行功能;一的onclick =someFunction()也不行

推荐答案

也许你应该使用 jQuery.getScript 来装载从服务器一些JavaScript并执行它。

Probably you should use jQuery.getScript to loading some javascript from the server and execute it.

更新: 在大多数情况下,一个负载仅就 jQuery.ajax 的纯HTML片段。元素结合到一些JavaScript函数之一做成功里面处理。所有的功能这在以前(装载任何事件处理一一使用<脚本> < HEAD> 主网页(其中要求在网页上的块) jQuery.ajax )。然后,所有看起来并不需要非常清晰,动态加载的任何脚本。

UPDATED: In the most cases one load only the pure HTML fragment with respect of jQuery.ajax. Binding of elements to some javascript functions one do inside of success handle. All functions which one use in any event handler one loaded before (with <script> in the <head> block) on the main page (on the page which call jQuery.ajax). Then all look very clear and dynamic loading of any scripts is not needed.