简体   繁体   English

我正在尝试使用js将多个功能和按钮连接在一起。 和html。 我不确定我缺少什么

[英]I am trying to connect multiple functions and buttons together using js. and html. and I'm not sure what i'm missing

When I open the Html file in the browser, it shows that the two are connected properly but it wont output anything I type into the box.I just need it to output the function result depending on which button was pressed. 当我在浏览器中打开Html文件时,表明两者已正确连接,但不会输出我在框中输入的任何内容,我只需要它根据按下哪个按钮来输出函数结果。 Any help or advice is appreciated. 任何帮助或建议,不胜感激。

 function reverseStr(s) { var o = []; for (var i = 0, len = s.length; i <= len; i++) o.push(s.charAt(len - i)); return o.join(''); } reverseStr("Hello"); var ch = "a"; var pattern =/a|e|i|o|u|y/i ; isVowelR(ch, pattern); function isVowelR(ch,pattern){ if(pattern.test(ch)){ return true; } else{return false} } function countVowles(str1) { var vowel_list = 'aeiouAEIOU'; var vcount = 0; for(var x = 0; x < str1.length ; x++) { if (vowel_list.indexOf(str1[x]) !== -1) { vcount += 1; } } return vcount; } console.log(countVowles("")); var pattern = /[0-9]/; function isDigit(regexp){ if(pattern.test(regexp)){ return true; } else{return false} } var main = function() { document.getElementByld("#inpt").value; //this keyword is set to the button that fired the event console.log(this.id); //dispatch on button id if (this.id == "btn1") document.querySelector("div").innerHTML = "You clicked Button1"; else if (this.id == "btn2") document.querySelector("div").innerHTML = "You clicked Button2"; else if (this.id == "btn3") document.querySelector("div").innerHTML = "You clicked Button3"; else document.querySelector("div").innerHTML = "You Clicked Button4"; }; //2. register the onclick handlers after the DOM is complete window.addEventListener("load", function() { //select the buttons var buttons = document.querySelectorAll("button"); //register the same handler for each button for (var i = 0; i < buttons.length; ++i) { buttons[i].addEventListener("click", main); } }); 
 <!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8' > <title> Buttons Demo </title> </head> <body> <h2>String Machine</h2> <p> Enter a String: </p> <input id= "inpt"> </input> <button id="btn1">Reverse</button> <button id="btn2">Vowel or no Vowel</button> <button id="btn3">Count Vowels</button> <button id="btn4">Digit or no Digit</button> <br> <hr style="width:23%; margin-left:0;"> <div id="outDiv"></div> <script src="stringMach.js"></script> </body> </html> 

This is working fine. 一切正常。 Hope it'll helps 希望对你有帮助

 function reverseStr(s) { var o = []; for (var i = 0, len = s.length; i <= len; i++) o.push(s.charAt(len - i)); return o.join(''); } reverseStr("Hello"); var ch = "a"; var pattern =/a|e|i|o|u|y/i ; isVowelR(ch, pattern); function isVowelR(ch,pattern){ if(pattern.test(ch)){ return true; } else{ return false } } function countVowles(str1) { var vowel_list = 'aeiouAEIOU'; var vcount = 0; for(var x = 0; x < str1.length ; x++) { if (vowel_list.indexOf(str1[x]) !== -1) { vcount += 1; } } return vcount; } var pattern = /[0-9]/; function isDigit(regexp){ if(pattern.test(regexp)){ return true; } else{return false} } var main = function() { //this keyword is set to the button that fired the event var str = document.getElementById('inpt').value; var pattern =/a|e|i|o|u|y/i ; var pattern2 = /[0-9]/; console.log(this.id); //dispatch on button id if (this.id == "btn1") document.querySelector("div").innerHTML = reverseStr(str); else if (this.id == "btn2") document.querySelector("div").innerHTML = isVowelR(str, pattern); else if (this.id == "btn3") document.querySelector("div").innerHTML = countVowles(str); else document.querySelector("div").innerHTML = "You Clicked Button4"; }; //2. register the onclick handlers after the DOM is complete window.addEventListener("load", function() { //select the buttons var buttons = document.querySelectorAll("button"); //register the same handler for each button for (var i = 0; i < buttons.length; ++i) { buttons[i].addEventListener("click", main); } }); 
 <!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8' > <title> Buttons Demo </title> </head> <body> <h2>String Machine</h2> <p> Enter a String: </p> <input id= "inpt"> </input> <button id="btn1">Reverse</button> <button id="btn2">Vowel or no Vowel</button> <button id="btn3">Count Vowels</button> <button id="btn4">Digit or no Digit</button> <br> <hr style="width:23%; margin-left:0;"> <div id="outDiv"></div> <script src="stringMach.js"></script> </body> </html> 

暂无
暂无

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

相关问题 函数没有定义,但是我很确定我定义了它。我想念什么? - function not defined, but i'm quite sure i defined it.. What am i missing? 从事一项任务,我只是不理解某些东西,但不确定我错过了什么(可能很多) - working on an assignment and I am just not understanding something but not sure what I'm missing (probably alot) 我正在尝试使用CodeMirror编辑器构建D3.js,但是我在那里输入的任何内容都无法通过D3.js正确显示。 我错过了什么? - I'm trying to build D3.js by using CodeMirror editor, but anything I input in there can't be properly visualized by D3.js. What am I missing? 试图显示API结果,不确定我缺少什么。 - Trying to get API results to display, not sure what I'm missing. 是否有一个令人敬畏的HTML / JS编辑器,我错过了? - Is there an awesome HTML/JS editor that I'm missing? 我正在尝试在JS中创建这个随机名称生成器。 数组返回undefined? - I'm trying to make this random name generator in JS. The array is returning undefined? 我究竟做错了什么? 我正在尝试克隆一些字段并使用一些函数 - What am I doing wrong? I'm trying to clone some fields and use some functions 我试图一键挂起 2 个功能,然后出现错误,我做错了什么? - I'm trying to hang 2 functions on one click, and getting an error, what am I doing wrong? 我不确定在Web上使这个应用程序在Google Cloud上运行正常 - I'm not sure what I'm missing to make this app work correctly on Google Cloud when it comes to Websockets 使用ng-repeat制作水平列表? 不知道我在这里想念的是什么 - Making a horizontal list using ng-repeat? Not sure what I'm missing here
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM