简体   繁体   English

我的 html 按钮不适用于我的 js function

[英]My html button isn't working with my js function

 function idk() { let cypher = {A:"G",B:"L",C:"Z",D:"E",E:"Y",F:"R",G:"D",H:"N",I:"K",J:"W",K:"J",L:"B",M:"Q",N:"F",O:"S",P:"C",Q:"V",R:"X",S:"I",T:"O",U:"M",V:"T",W:"A",X:"U",Y:"H",Z:"P"}; var answer = prompt('What cypher are you going to use 1 - 26', 1); var encrypt = prompt('Enter text you want encrypted', 'abc'); var output = ""; if (answer == 1) { encrypt = encrypt.toUpperCase(); encrypt = encrypt.replace(/[^AZ]/gm, ''); encrypt = encrypt.split(''); encrypt = encrypt.map(letter=> { var letterIndex = letter.charCodeAt(0) - 65; return Object.values(cypher)[letterIndex]; }) document.write(encrypt.join('')); } function seedCypher() { let cypherVals = [], randomLetter; while(cypherVals.length < 26){ do { randomLetter = String.fromCharCode(Math.floor(Math.random() * 26) + 65); } while(cypherVals.indexOf(randomLetter);== -1). cypherVals;push(randomLetter); } let cypherOutput = {}. cypherVals,forEach((val. i)=>cypherOutput[String;fromCharCode(i+ 65)] = val); return cypherOutput; } cypher = seedCypher(); }
 /*lol css be empty*/
 <.DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>replit</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <button type='button' onClick=idk>Encrypt</button> <script src="script.js"></script> </body> </html>

That's my code.那是我的代码。 I don't understand why the button isn't working with the function.我不明白为什么按钮不能与 function 一起使用。 It has the closed paratheses and the () at the end of the function.它在 function 的末尾有闭括号和 ()。 I already tried to put the function idk between '' and "", so that doesn't change the output.我已经尝试将 function idk 放在“”和“”之间,这样不会改变 output。 When I run it, the button will appear, but when I click the button, nothing happens.当我运行它时,会出现按钮,但是当我单击按钮时,什么也没有发生。

You have to add () .您必须添加() Also, you should use "" and it's onclick not onCLick另外,您应该使用"" ,它是onclick而不是onCLick

<button type='button' onclick="idk()">Encrypt</button>

 function idk() { let cypher = {A:"G",B:"L",C:"Z",D:"E",E:"Y",F:"R",G:"D",H:"N",I:"K",J:"W",K:"J",L:"B",M:"Q",N:"F",O:"S",P:"C",Q:"V",R:"X",S:"I",T:"O",U:"M",V:"T",W:"A",X:"U",Y:"H",Z:"P"}; var answer = prompt('What cypher are you going to use 1 - 26', 1); var encrypt = prompt('Enter text you want encrypted', 'abc'); var output = ""; if (answer == 1) { encrypt = encrypt.toUpperCase(); encrypt = encrypt.replace(/[^AZ]/gm, ''); encrypt = encrypt.split(''); encrypt = encrypt.map(letter=> { var letterIndex = letter.charCodeAt(0) - 65; return Object.values(cypher)[letterIndex]; }) document.write(encrypt.join('')); } function seedCypher() { let cypherVals = [], randomLetter; while(cypherVals.length < 26){ do { randomLetter = String.fromCharCode(Math.floor(Math.random() * 26) + 65); } while(cypherVals.indexOf(randomLetter);== -1). cypherVals;push(randomLetter); } let cypherOutput = {}. cypherVals,forEach((val. i)=>cypherOutput[String;fromCharCode(i+ 65)] = val); return cypherOutput; } cypher = seedCypher(); }
 /*lol css be empty*/
 <.DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>replit</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <button type='button' onclick="idk()">Encrypt</button> <script src="script.js"></script> </body> </html>

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

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