简体   繁体   English

动态添加label到HTML形成javascript

[英]Dynamically adding label to HTML form javascript

Hey guys so I was given this practical question in my university and I need some help trying to figure out somethings.大家好,我在大学里遇到了这个实际问题,我需要一些帮助来解决问题。

So this is the question given:所以这是给出的问题:

Display the radius and the ratio of area to circumference for all circles with integer radii beginning with a radius of 1 and continuing while the ratio is less than 30显示半径为 integer 的所有圆的半径和面积与周长的比率,从半径 1 开始,一直到比率小于 30

The output wanted is:通缉的output是:

Radius: 1, ratio: 0.5半径:1,比率:0.5

Radius: 2, ratio: 1半径:2,比率:1

Radius: 3, ratio: 1.5半径:3,比率:1.5

Radius: 4, ratio: 2半径:4,比率:2

Radius: 5, ratio: 2.5.半径:5,比率:2.5。 . . . . . . . . . . . . . . . . // Continue until condition is met // 继续直到满足条件

These are my codes: ( http://pastebin.com/0pgp0Bzj )这些是我的代码:( http://pastebin.com/0pgp0Bzj

 var radius=1, area, circum, ratio=0;; var radiusRef = document.getElementById("RadiusOutput"); var ratioRef = document.getElementById("RatioOutput"); var radiusOutput = "", ratioOutput = ""; var newRatioLabel = document.createElement("Label"); while (ratio<30) { circum = 2 * Math.PI * radius; area = Math.PI * (radius * radius); ratio = area/circum; radiusOutput = radius; ratioOutput = ratio + "<br/>" // NEED TO DYNAMICALLY ADD TWO NEW LABELS HERE SO THAT MY RADIUS AND RATIO WILL BE PRINTED ON THE FOLLOWING LINE AS PER LOOP var newLabel = document.createElement("label"); newLabel.appendChild("") radius = radius + 1; } radiusRef.innerHTML = radiusOutput; ratioRfef.innerHTML = ratioOutput;
 #outputArea { padding: .25em; border: solid black 2px; margin: 3em; height: 20em; width: 20em; overflow-y: scroll; font-family: consolas, 'courier new', monospace; font-size: 1em; color: rgb(50, 50, 250); background-color: rgb(225,225,225); }
 <div id="outputArea"> <p> Radius: <label id="RadiusOutput"></label>, ratio: <label id="RatioOutput"></label> </p> </div>

I want to dynamically add the labels for value of radius and ratio everytime my code is looped.每次循环我的代码时,我都想动态添加半径和比率值的标签。 If I am not mistaken it has something to do with createElement and appendChild but I can't seem to wrap my head around that concept (I am really noob at javascript and html)如果我没记错的话,它与 createElement 和 appendChild 有关,但我似乎无法理解这个概念(我真的是 javascript 和 html 的菜鸟)

You are using append child in wrong way.您正在以错误的方式使用 append 孩子。 Read from here first.首先从这里阅读。

To create a new label element,要创建一个新的 label 元素,

//radius and ratio variables are considered computed //半径和比率变量被认为是计算出来的

let mylabel = document.createElement('Label');
let labelValue = document.createTextNode("Radius :" + radius + " Ratio: " + ratio); 
mylabel.appendChild(labelValue);
document.getElementById('RatioOutput').appendChild(mylabel);

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

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