Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.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
jqueryforeach,选择多个跨距并添加到Div_Jquery_Html_Each - Fatal编程技术网

jqueryforeach,选择多个跨距并添加到Div

jqueryforeach,选择多个跨距并添加到Div,jquery,html,each,Jquery,Html,Each,我试图逐个循环遍历每个元素,并使用一些单独的html添加它,使用我的js,甚至使用foreach,它总是将所有元素作为一个实体发出警报。任何关于这里哪里出了问题的建议 $("#testDiv").html(""); $("#main span b").each(function () { alert($('#main span b').text()); $("#testDiv").append($("<span/>", { class: 'adk

我试图逐个循环遍历每个
元素,并使用一些单独的html添加它,使用我的js,甚至使用foreach,它总是将所有元素作为一个实体发出警报。任何关于这里哪里出了问题的建议

$("#testDiv").html("");
$("#main span b").each(function () {
     alert($('#main span b').text());
     $("#testDiv").append($("<span/>", {
         class: 'adkeys',
         html: $('#main span b').text() + "<a class='anch'>"
     }));
});
$(“#testDiv”).html(“”);
$(“#主跨度b”)。每个(功能){
警报($('#主跨度b').text());
$(“#testDiv”)。追加($(“”{
类:“adkeys”,

html:$('#main span b')。text()+“

在循环中使用
this
关键字,而不是再次使用选择器

$("#testDiv").html("");

$("#main span b").each(function () {
     alert($(this).text());

     $("#testDiv").append($("<span/>", {
         'class' : 'adkeys',
         html    : "<a class='anch'>" + $(this).text() + "</a>"
     }));

});
$(“#testDiv”).html(“”);
$(“#主跨度b”)。每个(功能){
警报($(this.text());
$(“#testDiv”)。追加($(“”{
“类”:“adkeys”,
html:“

您可以在循环体中使用$(this),也可以按以下方式编写

$('yourselector').each(function(index,value){
 alert($(value).text());
});