[英]Using javascript, how to add $ sign before any input element on an HTML page
使用javascript,如何在HTML页面上的任何输入字段之前添加“$”符号?
You can get all input field on page using document.getElementsByTagName('input')
and then use Node.insertBefore() function inside a loop to insert dollar sign $
before every one of them : 您可以使用document.getElementsByTagName('input')
获取页面上的所有输入字段,然后在循环内使用Node.insertBefore()函数在每个字段之前插入美元符号$
:
var inputs = document.getElementsByTagName('input'); for ( var i=0 ; i < inputs.length ; i++){ var text = document.createTextNode('$'); inputs[i].parentNode.insertBefore(text, inputs[i]); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" /><br/> <input type="text" /><br/> <input type="text" /><br/> <input type="text" /><br/> <input type="text" />
Hope this helps. 希望这可以帮助。
Alternatively you could use pure CSS to do so, by using the :before
or :after
pseudo elements and wrapping your input containers in div
's 或者,您可以使用纯CSS来执行此操作,方法是使用:before
或:after
伪元素并将输入容器包装在div
.inputContainer:before, .inputContainer::before { content: '$'; }
<div class="inputContainer"> <input type="text"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.