如何让这个“转换”按钮将文本从输入转换为二进制?

我正在尝试创建一个可以使用 JavaScript 将文本转换为二进制的基本网站,但我真的不知道如何使按钮Convert!工作。有人有我可以使用的简单修复方法吗?(别介意 CSS 缺少一些代码,我还没说完。)



function convert() {

  const input_el = document.querySelector(".input-text");

  const output_el = document.querySelector(".output-binary");


  input_el.addEventListener("input", (event) => {

    let input_text = event.target.value;

    let output_arr = [];

    

    for (var i = 0; i < input_text.length; i++) {

      output_arr.push(input_text.charCodeAt(i).toString(2));

    }

    

    output_el.innerHTML = output_arr.join(" ");

  });

}

body {

  margin: 0;

  padding: 0;

}


header {

  padding: 30px;

  background: #09A954;

  color: white;

  text-align: center;

  font-family: arial;

  font-size: 30px;

}


.navbar {

  padding: 12px;

  background: #363636;

  color: white;

  font-size: 15px;

}


.container {

  display: flex;

  flex-direction: column;

  padding: 80px;

  background: #B3B3B3;

}


.input_box {

  display: flex;

  flex-direction: column;

  justify-content: center;

  align-items: center;

  padding: 40px;

  background: #D35400;

  border-radius: 10px;

  text-align: center;

  box-shadow: 0px 3px 5px #000000;

  height: 100px;

  width: ;

}


input {

  background: #D35400;

  border: none;

  border-bottom: 2px solid #000000;

  font-size: 15px;

}


button {

  width: 100px;

}


.output_box {

  display: flex;

  flex-direction: column;

  justify-content: center;

  align-items: center;

  padding: 40px;

  background: #2980B9;

  border-radius: 10px;

  text-align: center;

  box-shadow: 0px 3px 5px #000000;

  height: 100px;

  width: ;

}


p2 {

  font-size: 30px;

  font-family: calibri;

}


p4 {

  font-size: 15px;

  font-family: arial;

}

链接到 JSFiddle:https://jsfiddle.net/SaltySandwich/65te8omy/


波斯汪
浏览 67回答 2
2回答

饮歌长啸

我修复了您现有的项目。我对 JavaScript 采取了一种更简单的方法。您可以在 JavaScript 代码上用 6 行编写您需要的所有内容:)对于您的输出,您应该使用<output>代替<input>.看看这个:function convert() {&nbsp; var output = document.getElementById("output_text");&nbsp; var input = document.getElementById("input_text").value;&nbsp; output.value = "";&nbsp; for (i = 0; i < input.length; i++) {&nbsp; &nbsp; output.value += input[i].charCodeAt(0).toString(2) + " ";&nbsp; }}body {&nbsp; margin: 0;&nbsp; padding: 0;}header {&nbsp; padding: 30px;&nbsp; background: #09A954;&nbsp; color: white;&nbsp; text-align: center;&nbsp; font-family: arial;&nbsp; font-size: 30px;}.navbar {&nbsp; padding: 12px;&nbsp; background: #363636;&nbsp; color: white;&nbsp; font-size: 15px;}.container {&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; padding: 80px;&nbsp; background: #B3B3B3;}.input_box {&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; justify-content: center;&nbsp; align-items: center;&nbsp; padding: 40px;&nbsp; background: #D35400;&nbsp; border-radius: 10px;&nbsp; text-align: center;&nbsp; box-shadow: 0px 3px 5px #000000;&nbsp; height: 100px;&nbsp; width: ;}input {&nbsp; background: #D35400;&nbsp; border: none;&nbsp; border-bottom: 2px solid #000000;&nbsp; font-size: 15px;}input:focus {&nbsp; border: none;&nbsp; border-bottom: 2px solid #000000;&nbsp; font-size: 15px;&nbsp; outline: none;}button {&nbsp; width: 100px;}.output_box {&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; justify-content: center;&nbsp; align-items: center;&nbsp; padding: 40px;&nbsp; background: #2980B9;&nbsp; border-radius: 10px;&nbsp; text-align: center;&nbsp; box-shadow: 0px 3px 5px #000000;&nbsp; height: 100px;&nbsp; width: ;}p2 {&nbsp; font-size: 30px;&nbsp; font-family: calibri;}p4 {&nbsp; font-size: 15px;&nbsp; font-family: arial;}<!DOCTYPE html><html><head>&nbsp; <title>Text to binary converter</title>&nbsp; <meta charset="utf-8">&nbsp; <link rel="stylesheet" href="style.css"></head><body>&nbsp; <header>&nbsp; &nbsp; <h1>Text to binary converter</h1>&nbsp; </header>&nbsp; <div class="navbar">&nbsp; &nbsp; <p1>link</p1>&nbsp; </div>&nbsp; <div class="container">&nbsp; &nbsp; <div class="input_box">&nbsp; &nbsp; &nbsp; <p2>This is what a human sees:</p2><br>&nbsp; &nbsp; &nbsp; <p3>Please enter text below</p3><br>&nbsp; &nbsp; &nbsp; <!--INPUT FIELD-->&nbsp; &nbsp; &nbsp; <input id="input_text" value="Human sees this" />&nbsp; &nbsp; </div><br><br>&nbsp; &nbsp; <button onclick="convert()">Convert!</button><br><br>&nbsp; &nbsp; <div class="output_box">&nbsp; &nbsp; &nbsp; <p2>This is what a machine sees:</p2><br>&nbsp; &nbsp; &nbsp; <!--INPUT FIELD-->&nbsp; &nbsp; &nbsp; <output id="output_text">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script src="main.js"></script></body></html>哦..我还稍微编辑了你的CSS,这样输入周围就没有烦人的边框了...你可以通过删除以下内容来删除我的更改:&nbsp; input:focus {&nbsp; &nbsp; border: none;&nbsp; &nbsp; border-bottom: 2px solid #000000;&nbsp; &nbsp; font-size: 15px;&nbsp; &nbsp; outline: none;&nbsp; }如果您需要清除任何内容,请务必在这篇文章下发表评论:)

慕沐林林

看看这个:function convert() {&nbsp; &nbsp; var&nbsp; output=document.getElementById("ti2");&nbsp;&nbsp;&nbsp; &nbsp; var input=document.getElementById("ti1").value;&nbsp; &nbsp; &nbsp; output.value = "";&nbsp; &nbsp; &nbsp; for (i=0; i < input.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;output.value +=input[i].charCodeAt(0).toString(2) + " ";&nbsp; &nbsp; &nbsp; }&nbsp; }input {font-size:12px; width:200px;}<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; &nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="IE=edge">&nbsp; &nbsp; &nbsp; &nbsp; <title></title>&nbsp; &nbsp; &nbsp; &nbsp; <meta name="description" content="">&nbsp; &nbsp; &nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1">&nbsp; &nbsp; &nbsp; &nbsp; <link rel="stylesheet" href="">&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <input id="ti1" value ="Human sees this"/>&nbsp; &nbsp; &nbsp; &nbsp; <input id="ti2" value ="Machine sees this"/>&nbsp; &nbsp; &nbsp; &nbsp; <button onclick="convert();">Convert!</button>&nbsp; &nbsp; &nbsp; &nbsp; <script src="main.js"></script>&nbsp; &nbsp; </body></html>它应该给你基本的想法......
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript