[英]Results Preview in Multiple Input or Textarea Not Running
I have the code to display a preview of the code tag on a form. 我有代码可以在表单上显示代码标签的预览。
If you only use one input, this can be. 如果仅使用一个输入,则可以。 But if you add one more input, then it is not running. 但是,如果再添加一个输入,则该输入将无法运行。
My code: 我的代码:
<style>
.text_edit, #prevTxt {
width: 400px;
height:200px;
border: solid 1px #D4DEDE;
padding: 10px;
}
</style>
<script type="text/javascript">
function prevTxt() {
var textArea = document.getElementById('inputTxt');
var div = document.getElementById('prevTxt');
var text = textArea.value;
text = text.replace(/\n/gi, "<br />");
div.innerHTML = text;
}
</script>
<textarea class="text_edit" id="inputTxt" onkeyup="prevTxt()"></textarea>
<div id="prevTxt" style="background: #E7ECEC; line-height: 1.1em;"></div>
If I add this code, you can not run 如果添加此代码,则无法运行
<input class="text_edit" id="inputTxt" onkeyup="prevTxt()" />
How to change the code? 如何更改代码?
You should use unique ids. 您应该使用唯一的ID。 You can rewrite you code without using ids here: 您可以在不使用ID的情况下重写代码:
<textarea class="text_edit" onkeyup="prevTxt(this)"></textarea>
<input class="text_edit" onkeyup="prevTxt(this)" />
function prevTxt(el) {
var div = document.getElementById('prevTxt');
var text = el.value;
text = text.replace(/\n/gi, "<br />");
div.innerHTML = text;
}
You cannot use duplicate ID's across a single page. 您不能在单个页面上使用重复的ID。 Since you already used "inputTXT", you will have to use a different id, but it looks like you're using that input tag for the same purpose as the previously used textarea. 由于您已经使用过“ inputTXT”,因此您将不得不使用不同的id,但看起来您正在使用该input标签的目的与先前使用的textarea相同。 You will need to get rid of or rename one of them. 您将需要摆脱或重命名其中之一。
Pick anything you want for the id. 选择任何您想要的ID。 For instance, this would work: 例如,这将起作用:
<input class="text_edit" id="inputTxtInput" onkeyup="prevTxt()" />
The attribute ID in HTML has to be unique. HTML中的属性ID必须是唯一的。 If you want to create another element tag, you'll have to change the ID. 如果要创建另一个元素标签,则必须更改ID。 For example: 例如:
<textarea class="text_edit" id="inputTxt" onkeyup="prevTxt()"></textarea> <div id="prevTxt" style="background: #E7ECEC; line-height: 1.1em;"> <input id="inputTxt2"/> <input id="inputTxt3"/> <input id="inputTxt4"/> </div>
If you want to apply the same effects/attribute to multiple elements, use the attribute class . 如果要将相同的效果/属性应用于多个元素,请使用属性类 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.