【发布时间】:2015-10-28 07:41:23
【问题描述】:
我有一个代码,它显示用户正在输入的内容,并且该输入被写回到 html 文档中的 a 中。
这是下面的代码:
<html><head>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
<script src="jquery.zclip.js"></script>
<style> body{font-family:century gothic;}</style>
</head>
<body style="zoom: 1;">
<div style="padding:10%;">
<textarea type="text" name="fname" class="chatinput" style="margin: 0px; width: 977px; height: 324px;"> </textarea>
<div style="padding-top:10%;" class="printchatbox"></div>
<script type="text/javascript" src="jquery-1.8.0.min.js"></script>
<script src="jquery.zclip.js"></script>
<script type="text/javascript">
$('.chatinput').keyup(function(event) {
newText = event.target.value;
$('.printchatbox').text(newText);
});
</script>
有人可以帮我弄清楚如何制作它,以便在用户键入时将一个单词或一行字符串替换为其他内容。
例如,用户写“这是一个字符串”......程序将其写在 html 文档上,但还会自动将该特定字符串从“这是一个字符串”更改为“这是一个文本” “.. 请帮忙!
【问题讨论】:
-
我现在不能给你一个准确的代码响应,但是一个(有些不雅的)解决方案是将输入框变成一个在按键时更新的字符串。然后,使用 for 循环遍历单词的关联数组及其替换。使用 .indexOf 方法在输入字符串中查找单词,然后拆分输入并从数组中连接所需的替换。
标签: javascript jquery css arrays html