Javascript jQuery load()回调在没有警报()的情况下失败

Javascript jQuery load()回调在没有警报()的情况下失败,javascript,jquery,html,jquery-callback,Javascript,Jquery,Html,Jquery Callback,我正在将一个html片段从网站上的另一个页面加载到一个content div中。在load回调中,我正在加载新内容特有的JS 在$.getScript之后使用alert(),一切正常。如果没有alert(),setup()将无法运行 我尝试使用setTimeout/cleartimout等待 我已尝试调用$(document).ready 我已经尝试过使用新的JS(没有getScript) 没有快乐。简化示例如下: <html> <head><!--

我正在将一个html片段从网站上的另一个页面加载到一个content div中。在load回调中,我正在加载新内容特有的JS

$.getScript
之后使用
alert()
,一切正常。如果没有
alert()
setup()
将无法运行

  • 我尝试使用
    setTimeout/cleartimout
    等待
  • 我已尝试调用
    $(document).ready
  • 我已经尝试过使用新的JS(没有getScript) 没有快乐。简化示例如下:

       <html>
        <head><!-- load jQuery, stylesheets, etc.--> </head>
        <body>
    
        <div id="menupanel">
            <!-- menu clicks call jQuery: $('#content').ajaxLoad( myURL ). -->
        </div>
    
        <div id="content">
            <!-- Content is inserted here by menu clicks.-->
        </div> 
    
        <script type="text/javascript">
        $( function(){
    
            //Function to load a fragment from another page into this page.
            function ajaxLoad(url,parms){
              var url= url + ' #remoteContent';
              $("#content").load( url, parms, callback(sometext) );
            }
    
          function callback(sometext) {
            //unbind all events on the #content div. 
            $('#content').off();  
    
            //run scripts specific to newly loaded page
            //and bind new events to elements on the new page.
            $.getScript('getTest.js',function(){    
              alert('bingo');   //setup() won't run w/o this alert.  
              setup();   
            });
          } 
          //handle menu clicks.
          $("#menu a").click(function(e){
             //etc...
          });
    
        })  //document ready
    
        </script>
    
        </body>
        </html>
    

    您需要知道将函数作为参数传递与将函数结果作为参数传递之间的区别<代码>$(“#内容”).load(url、parms、callback())应该是
    $(“#content”).load(url、parms、callback)很抱歉造成混淆。我实际上在回调中传递了一个参数,但在编辑/缩短的代码中没有显示。现在修复了。这就是问题所在:使用
    ()
    调用函数,不管是否使用参数,执行函数并传递其结果,而不传递函数本身。如果要将参数传递给函数,请将其包装在匿名函数中,就像使用
    $.getScript('getTest.js',function(){
    --
    $(“#content”).load(url、parms、function(){callback(someText)})一样;
    很抱歉造成混淆
    -我一点也不困惑,不正确的代码是不正确的,添加一个参数并不能改变它有多不正确!这绝对是个问题。谢谢-很明显,混淆完全在我这一边。现在可以正常工作了。
        function setup(){
          var myCount= $("#content").find(".subhead_title").length;
    
          $("#content #remoteContent #lotPage #lotText").find(".subhead_title").css('color','green');
    
          console.log('count of subhead titles: '+myCount); 
        }