简体   繁体   English

使用javascript时,文本继续在输入框的右侧

[英]Text continues out the right side of input box when using javascript

When entering text into the input box it continues outside the box and can not be seen, you can only see the text you have already written in the box.当在输入框中输入文本时,它会继续在框外并且无法看到,您只能看到您已经在框中输入的文本。 The text does not go outside the box if I comment out the javascript section so I know it is not a css issue but rather a javascript issue.如果我注释掉 javascript 部分,文本不会超出框,所以我知道这不是 css 问题,而是 javascript 问题。 I could live with it but I am sure other people would complain if they use it.我可以忍受它,但我相信其他人如果使用它会抱怨。

 var input = document.getElementById("text"); input.addEventListener("input", function(event) { var MessageString = document.querySelector('#text').value; // let look = convertToSmilies(MessageString); // var MessageString = look; input.value = ""; input.value += MessageString; });
 div#sendCtrls { width: auto; margin: 5px auto; } div#sendCtrls input, div#sendCtrls button{ width: 50%; margin: 5px auto; background: none repeat scroll 0 0 #F5F5F5; border: 1px solid #E0E0E0; padding: 0.625rem; } div#sendCtrls input:hover, div#sendCtrls button:hover{ width: 50%; margin: 5px auto; background: none repeat scroll 0 0 #FFFFFF; border: 1px solid #FFCCCC; padding: 0.625rem; }
 <div id="sendCtrls"> <input type="text" placeholder="Your message here" id="text"> <button id="myBtn" style="width: auto;"> Send </button> </div>

I commented out 2 lines in the JS because it was not needed for testing, it is just a smiley replacer.我在 JS 中注释掉了 2 行,因为测试不需要它,它只是一个笑脸替换器。 It will check what is typed in to the input box and if a pattern is matched it will replace the pattern in the input box with a smiley.它将检查输入框中输入的内容,如果匹配模式,它将用笑脸替换输入框中的模式。 The issue with text going out of the box is there no matter if those two lines are commented out or not.无论这两行是否被注释掉,文本开箱即用的问题都存在。 If I comment out the whole of the JS section the text doesnt go out of the box but then the smiley replacer will not work.如果我注释掉整个 JS 部分,文本不会开箱即用,但笑脸替换器将不起作用。

Uncomment these 2 lines of code lines 304 and 305取消注释这 2 行代码行 304 和 305

var input = document.getElementById("text");
input.addEventListener("input", function(event) {
    
    var MessageString = document.querySelector('#text').value;

//      let look = convertToSmilies(MessageString); //here
//      var MessageString = look;                       //and here

    document.querySelector('#text').value = "";
    document.querySelector('#text').value += MessageString;
});

Finally I can lay this code to rest!最后我可以放下这段代码休息了! The solution was rather simple and needed to replace the lines..解决方案相当简单,需要更换线路..

   input.value = "";
   input.value += MessageString;

with the line..与线..

input.value = MessageString;

I really hope this solution helps someone who might also be having similar problems.我真的希望这个解决方案可以帮助那些可能也有类似问题的人。 The final JS code is as follows..最终的JS代码如下..

var input = document.getElementById("text");
input.addEventListener("input", function(event) {
    
    var MessageString = document.querySelector('#text').value;

    let look = convertToSmilies(MessageString);
    var MessageString = look;

    input.value = MessageString;
});

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

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