简体   繁体   English

如何将正文添加到 JavaScript 部分?

[英]How can I add body to JavaScript section?

I am a new Javascript learner.我是一个新的 Javascript 学习者。 This code had grabbed my interest.这段代码引起了我的兴趣。 How can I remove what is inside the sections and add it to the sections.如何删除部分内的内容并将其添加到部分中。 In other words, I want it to be a full Javascript code.换句话说,我希望它是一个完整的 Javascript 代码。 Can anyone please lead or teach me to the right path or give me an example of how the code is going to look like.任何人都可以引导或教我正确的道路,或者给我一个代码看起来如何的例子。 Thanks谢谢

Body:身体:

<body>
    <p id="demo"></p>
    <p id="demoNew"></p>
</body>

JavaScript: JavaScript:

<script>
    var n = parseInt(prompt("How many numbers will be entered(max 20)-"));
    var numArray = [];
    for (var i = 0; i < n; i++) {
        numArray[i]=parseInt(prompt("Enter number between 10 and 100 "));
    }
    var uniqueNum = [...new Set(numArray)];
    document.getElementById("demo").innerHTML = "Count of items entered: "+n;
    document.getElementById("demoNew").innerHTML = "Numbers-dduplicates removed: "+uniqueNum;
</script>

Your code looks like getting 1~20 inputs between integer 10 and 100 from prompt and make a set with them but you want this without HTML.您的代码看起来像是从提示符中获得 integer 10 和 100 之间的 1~20 个输入并与它们进行设置,但您希望没有 HTML。

 var p = document.createElement("p"); p.setAttribute("id", "demo"); document.getElementsByTagName('body')[0].appendChild(p); p = document.createElement("p"); p.setAttribute("id", "demoNew"); document.getElementsByTagName('body')[0].appendChild(p); //create element and append to body as a child var n = -1; while(n<1 || n>20) { n = parseInt(prompt("How many numbers will be entered(between 1 and 20)")); } var numArray = []; var input = 0; for (var i = 0; i < n; i++) { input = parseInt(prompt("Enter #"+(i+1)+" number between 10 and 100 ")); if(input>=10 && input<=100) { numArray.push(input); } else { alert("invalid input"); i--; } } var uniqueNumArray = new Set(numArray); console.log(uniqueNumArray.size); document.getElementById("demo").innerHTML = "Count of items entered: "+n; document.getElementById("demoNew").innerHTML = "Numbers-duplicates removed: "+Array.from(uniqueNumArray);

暂无
暂无

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

相关问题 我可以创建不使用Javascript的HTML弹出窗口吗? 或者我可以将Javascript放入 <body> 部分? - Can I create an HTML pop up that do not use Javascript? or can I put the Javascript into the <body> section? 如何仅在主页上使用 JavaScript 将类动态添加到“正文”? - How can I dynamically add a class to 'body' using JavaScript only for the home page? 我可以通过Javascript函数将图像添加到HTML正文吗? - Can I add an image to an HTML body from a Javascript function? 如何添加 <thead> 带有tagName:&#39;table&#39;的木偶collectionview的部分? - How can I add a <thead> section to a marionette collectionview with tagName: 'table'? 如何在HTML div的innerHTML部分内添加按钮? - How can I add a button within an innerHTML section of a HTML div? 如何使用 JavaScript 向 html 正文添加内容? - How do I add content to html body using JavaScript? 如何使用CSS或JavaScript仅显示视频帧的一部分? - How can I show only a section of a video frame with CSS or JavaScript? 如何在javascript部分中使用django模板变量? - how can I use django templates variable in javascript section? 我想在视频部分下添加一个部分,但它在第一部分下并没有完全对齐。 我该如何解决这个问题? - I want to add a section under a video section but it doesn't align perfectly under the first section. How can I solve this? 我可以使用 javascript 修改 PDF 的 Javascript 部分吗 - Can I modify the Javascript section of a PDF with javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM