简体   繁体   English

如何在jQuery .html()函数中打印javascript数组值?

[英]How to print a javascript array values in a jQuery .html() function?

I have a JavaScript array that contains a set of strings. 我有一个包含一组字符串的JavaScript数组。 I want to display them in a HTML div element by line by line using j Query or JavaScript. 我想使用j Query或JavaScript在HTML div元素中逐行显示它们。

My code is up to now: 我的代码到现在为止:

var data = data;
for (i = 1; i <= data.length; i++) {

  data[i] = data[i] + '<br />';

  $(target).html('<a>'+data[i]+'</a>');
}

My data is displayed in this moment right now. 此时此刻我的数据就会显示出来。

Labelling MachinesLabels - Plastic, Metal, Foil etcLabels FabricLaboratories - MedicalLaboratories - TestingLaboratory Equipment & SuppliesLaboratory Equipment Services & Calibration

I want them displayed like this as links (inside tags): 我希望它们像这样显示为链接(内部标签):

Labelling Machines
Labels - Plastic, Metal, Foil etc
Labels Fabric
Laboratories - MedicalLaboratories - Testing
Laboratory Equipment & Supplies
Laboratory Equipment Services & Calibration

Thanks in advance 提前致谢

You should add the breaks outside of the link tags and use .html() only once, as it completely replaces the innerHTML of the given element, ie 您应该将中断符添加到链接标记之外,并且只能使用.html()一次,因为它完全替换了给定元素的innerHTML,即

str = "";
for (i = 1; i <= data.length; i++) {
    str += "<a>" + data[i] + "</a><br />";
}
$(target).html(str);

I would suggest another approach, to use innerHTML (javascript) or append (jquery) as another answer has already mentioned 我建议另一种方法,使用innerHTML(javascript)或append(jquery),因为已经提到了另一个答案

for (i = 1; i <= data.length; i++) {
    target.innerHTML += "<a>" + data[i] + "</a><br />";
}
var data = data;
var str = '';
for (var i = 1; i <= data.length; i++) {

  str += `<a>${data[i]}<br /></a>`;

}
$(target).html(str);

Try this. 尝试这个。

The cleanest way will be wrapped in a div. 最干净的方法将被包装在div中。 And you need to use .append() method to not override the initial data that is already added to the target. 并且您需要使用.append()方法不覆盖已经添加到目标中的初始数据。

 var data = ["Hello", "World", "Lorem", "Ipsum", "More length"]; for (var i = 0; i < data.length; i++) { $("#result").append('<div><a href="#" class="link">' + data[i] + '</a></div>'); } 
 .link { color: #5ca5cc; margin-bottom: 10px; text-decoration: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result"></div> 

Clean and more simple code along with working demo. 干净,更简单的代码以及有效的演示。

Your code is incomplete here.Not sure if you have declare variable i anywhere in code.Also you are starting to loop from 1st index 您的代码在这里是不完整的。不确定您是否在代码中的任何地方声明了变量i 。您也将从第一个索引开始循环

Instead of appending to DOM on every iteration,create a string and concat the value to it. 与其在每次迭代中都附加到DOM,不如创建一个字符串并将其值连接到它。 Append it on completion of the iteration. 在迭代完成时附加它。

var data = data,
htmlString="";
for (var i = 0; i <= data.length; i++) {
htmlString+= data[i] + '<br />';
}
$(target).append(htmlString);

Instead of a for/loop you could use ES6 in one line with map and a template literal . 代替for/loop您可以在带有maptemplateliteral的一行中使用ES6。

$(target).html(arr.map(el => `<a>${el}</a><br/>`));

DEMO DEMO

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

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