简体   繁体   English

jquery.each在jquery.load之后不起作用

[英]jquery.each not working after jquery.load

I use jQuery.load to load the HTML template. 我使用jQuery.load加载HTML模板。 After this I'm trying to get HTML content from each loaded HTML element. 之后,我尝试从每个加载的HTML元素中获取HTML内容。 The HTML is loading but I can't get the HTML content. HTML正在加载,但是我无法获取HTML内容。

Here is the code: 这是代码:

var _InterfaceBuilder = function() {
  var k45 = new _K45Kit;
  var _this = this;

  this.build = function(element) {
    var error = false;
    switch(element) {
      case 'loginPanel':
        $('#content').load('template/loginPanel.html', _this.localize(element));
        break;
        //sth else
    }   
    // sth else
  };

  this.localize = function(section) {           
    $(".loginPanel.localString").each(function(index) {
      console.log($(this).html());
    });

    //sth else
  });

When I put 当我放

$(".loginPanel.localString").each(function(index) {
  console.log($(this).html());
});

into the firebug console it works correctly. 到Firebug控制台中,它可以正常工作。 Can someone help me? 有人能帮我吗?

The 2nd parameter for $.load() must be a function that will be called once completion. $ .load()的第二个参数必须是一个在完成后将被调用的函数。 You are not providing a function, but the result of calling _this.localize(element). 您没有提供函数,而是提供了_this.localize(element)的结果。 So basically, the localize function is called before adding the listener, and since it returns undefined you have no handler. 因此,基本上,在添加侦听器之前会调用localize函数,由于该函数返回undefined,因此您没有处理程序。

Try with: 尝试:

$('#content').load('template/loginPanel.html', 
  function(){
    _this.localize(element);
});

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

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