简体   繁体   English

.html中的.append在jQuery中不起作用

[英].append inside .html is not working in jquery

I am trying to display multiple data using .append using for loop. 我正在尝试使用.append使用for循环显示多个数据。 I need all data to be replaced by second click to some other data by .html function. 我需要将所有数据替换为第二个单击.html函数的其他数据。

This is my code for more explanation. 这是我的代码,用于更多说明。

$(".content").html(function(){
  for(var l=1; l<4 ; l++)
    $(".content").append("Event-"+l+"<br>"+player.activity[l].desc+"<br>");
 }

suppose I have activity json like below: 假设我有如下活动json:

player : [{
"name": "Johny", 
"activity" :  [
    {"id:1","desc" : "abc"},
    {"id:2","desc" : "abc"},
    {"id:3","desc" : "abc"}
 ]
},{
"name": "Mony", 
"activity" :  [
    {"id:1","desc" : "abc"},
    {"id:2","desc" : "abc"},
    {"id:3","desc" : "abc"}
]
}]

So basically I want to replace my Johny's activity to mony's. 因此,基本上我想将Johny的活动替换为mony的活动。

I also tried swapping the methods such as .html inside .append which is working fine but .appnd inside .html is not working. 我也尝试过交换.append内的.html之类的方法,该方法可以正常工作,但.html内的.appnd则无法工作。 It is appending the Mony's data to johny's data. 它将Mony的数据附加到johny的数据。

Any suggestion? 有什么建议吗?

Functions is not allowed in the .html method. .html方法中 不允许使用功能。 Only strings. 仅字符串。

Your for should do exactly what you want. 您的 for应该完全按照您想要的去做。 Do not place inside .html 's function: 不要放在 .html的函数中:

 
 
 
  
  for(var l=1; l<4 ; l++) $(".content").append("Event-"+l+"<br>"+player.activity[l].desc+"<br>"); }
 
  

If you want to replace the content then just call .html with empty string before for : 如果您想更换内容,那么只需要调用.html与之前空字符串for

 $(".content").html(""); for(var l=1; l<4 ; l++) $(".content").append("Event-"+l+"<br>"+player.activity[l].desc+"<br>"); } 

UPDATE 更新

It's better to build a string and change DOM only once. 最好只构建一个字符串并只更改一次DOM。 This will reduce DOM access. 这将减少DOM访问。

 var html = ""; for(var l=1; l<4 ; l++) html += "Event-"+l+"<br>"+player.activity[l].desc+"<br>"; } $(".content").html(html); 

UPDATE #2 更新#2

Functions allowed but return should be used: 允许但return函数应使用:

 $(".content").html(function(){ var html = ""; for(var l=1; l<4 ; l++) html += "Event-"+l+"<br>"+player.activity[l].desc+"<br>"; } return html; }); 

This code is very much buggy. 这段代码有很多错误。

1) .html() should have parameter as string not a function. 1).html()应该将参数作为字符串而不是函数。

2) Player is array player.activity[l] won't work. 2)播放器是数组播放器。activity[l]无法正常工作。

If you want to see all description : following is the solution 如果您想查看所有描述:以下是解决方案

for(var i=0; i<player.length ; i++){
     for(var j=0; j<player[i].length ; j++){

        $(".content").append("Event-"+j+"<br>"+player[i].activity[j].desc+"<br>");
     }
    }

The documentation says: 文件说:

As of jQuery 1.4, the .html() method allows the HTML content to be set by passing in a function 从jQuery 1.4开始, .html()方法允许通过传入函数来设置HTML内容。

So, yes, you may use a function inside .html() . 因此,是的,您可以在.html()使用一个函数。 The function must return a string, which will be the content of the element. 该函数必须返回一个字符串,该字符串将是元素的内容。

$(".content").html(function(){
    var content = "";
    for (var l = 1; l < 4 ; l++) {
        content += "Event-" + l + "<br>" + player.activity[l].desc + "<br>";
    }
    return content;
 });

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

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