Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $。每个传递给函数的值_Javascript_Jquery_Arrays - Fatal编程技术网

Javascript $。每个传递给函数的值

Javascript $。每个传递给函数的值,javascript,jquery,arrays,Javascript,Jquery,Arrays,我不知道如何在Stackoverflow中搜索这个问题,它提供了不同的解决方案,与我的问题无关。所以我需要你们的帮助 变量obj的值: HTML文件: 实现以下功能: 所以问题是,当div出现时,它只输出: <div> <!-- Imagine that this content is inside a DIV --> **You Obtained Title2 Description2** </div> 但我的愿望是: <d

我不知道如何在Stackoverflow中搜索这个问题,它提供了不同的解决方案,与我的问题无关。所以我需要你们的帮助

变量obj的值:

HTML文件:

实现以下功能:

所以问题是,当div出现时,它只输出:

    <div> <!-- Imagine that this content is inside a DIV -->
**You Obtained Title2 Description2**

    </div>
但我的愿望是:

    <div> <!-- Imagine that this content is inside a DIV -->

**You Obtained Title1 Description1**

**You Obtained Title2 Description2**

    </div>
希望你们能帮助我,谢谢。

html函数正在替换div的全部内容,而不仅仅是添加

一个解决办法是替换

$("#achievement_popup").html(data).show(); 

但是有点重。就我个人而言,我会首先构建html并只使用html函数一次。

html函数将替换div的全部内容,而不仅仅是添加

一个解决办法是替换

$("#achievement_popup").html(data).show(); 

但是有点重。就我个人而言,我会首先构建html并只使用一次html函数。

使用append添加到元素末尾,而不是用html完全替换内容

或者,如果要同时用所有新内容替换现有内容:

/* start with empty string*/
var achievement='';
$.each(obj,function(index, item){
    /* add to string on each pass of loop - can use any html you want here*/
     achievement += "You Obtained " + item.title + " " + item.description +'<br>';
});
/* update element once all data parsed*/
 achievement_pop(achievement);
从处理的角度来看,解析字符串并进行一次更新是最有效的

使用append添加到元素末尾,而不是用html完全替换内容

或者,如果要同时用所有新内容替换现有内容:

/* start with empty string*/
var achievement='';
$.each(obj,function(index, item){
    /* add to string on each pass of loop - can use any html you want here*/
     achievement += "You Obtained " + item.title + " " + item.description +'<br>';
});
/* update element once all data parsed*/
 achievement_pop(achievement);
从处理的角度来看,解析字符串并进行一次更新是最有效的

$("#achievement_popup").html($("#achievement_popup").html()+data).show(); 
/* start with empty string*/
var achievement='';
$.each(obj,function(index, item){
    /* add to string on each pass of loop - can use any html you want here*/
     achievement += "You Obtained " + item.title + " " + item.description +'<br>';
});
/* update element once all data parsed*/
 achievement_pop(achievement);