简体   繁体   English

从JavaScript打印HTML上的文本

[英]Print text on HTML from JavaScript

I have this for loop 我有这个for循环

<script>
...
for(i = 0;i < json.length;i++){      
  document.getElementById("pText").innerHTML = json[i].name;
  document.getElementById("pLink").setAttribute("href",json[i].html_url);
}
</script>

I want to print a paragraph with a href on each loop, so i did this: 我想在每个循环上打印一个带有href的段落,所以我这样做了:

</script>

<a id="pLink">
   <p id="pText">
   </p>
</a>

It works but the thing is this only prints the last loop. 它可以工作,但事情是这只打印最后一个循环。 So i tried this inside the script 所以我在剧本中尝试了这个

document.write("<a href=\"" + json[i].html_url + "\">");
document.write("<p>" + json[i].name + "</p>");
document.write("</a>");

instead of this: 而不是这个:

document.getElementById("pText").innerHTML = json[i].name;
document.getElementById("pLink").setAttribute("href",json[i].html_url);

And it prints everything i want but it replaces the whole page. 它打印我想要的一切,但它取代了整个页面。 How can i do this? 我怎样才能做到这一点? Do i need to create an id for every loop? 我需要为每个循环创建一个id吗? Like "pText1, pText2, etc. "pText1, pText2, etc.

Create a container element for that loop, and add the html as you had in mind 为该循环创建一个容器元素,并按照您的想法添加html

<div id="container"></div>

Then in javascript 然后在javascript中

var container = document.getElementById('container');
var my_html = '';
for(var i = 0;i < json.length;i++){  
  my_html += '<a href="' + json[i].html_url + '\">';
  my_html += '<p>'+  json[i].name + '</p>'
  my_html += '</a>'
}
container.innerHTML = my_html;

What we are doing here is adding the content to a string as many times as needed and then add it to the container so it already has all the loops 我们在这里做的是根据需要将内容添加到字符串中多次,然后将其添加到容器中,以便它已经具有所有循环

 document.getElementById("pText").innerHTML = json[i].name; document.getElementById("pLink").setAttribute("href",json[i].html_url); 

If you want to use your this code, you have to write "+=" instead of the "=". 如果要使用此代码,则必须编写“+ =”而不是“=”。

 var json = [ {"name":"Name 1", "html_url": "http://www.example.com"}, {"name":"Name 2", "html_url": "http://www.example.com"}, {"name":"Name 3", "html_url": "http://www.example.com"} ]; for(var i = 0; i < json.length; i++){ document.getElementById("pText").innerHTML += json[i].name + "<br>"; document.getElementById("pLink").setAttribute("href",json[i].html_url); } 
 <a id="pLink"> <p id="pText"> </p> </a> 

I will do it in the following way: 我将通过以下方式完成:

 let json = [{'name':'Google','html_url':'https://www.google.com/'}, {'name':'Facebook','html_url':'https://www.facebook.com/'}, {'name':'Twitter','html_url':'https://twitter.com/?lang=en'}]; let item = document.querySelector(".pLink") for(let j = 1; j<json.length; j++){ let cln = item.cloneNode(true); document.body.appendChild(cln); } let aTag = document.querySelectorAll('a.pLink'); aTag.forEach(function(item, i){ let a = item.setAttribute("href",json[i].html_url); let p = item.querySelector('.pText'); p.innerHTML = json[i].name; }) 
 <a class="pLink"> <p class="pText"> </p> </a> 

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

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