繁体   English   中英

如何在`textarea`或`textbox`中更改运行时的`keyword`颜色? 使用Javascript

[英]How can i change `keyword` color on runtime in `textarea` or `textbox`? Javascript

我有一个textarea我希望当我键入像var这样的关键字并按下SPACE按钮然后它的颜色将在运行时变为blue 。我定义了许多关键字我的自我。没有按钮点击它将在运行时。我怎么能这样做?谢谢。我也想在代码codebehind这个textarea文本。我在ASP.NET C#环境中工作。就像SQL-QUERY-EDITOR 这是我的代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" runat="server">
var codeInput = document.getElementsByTagName("textarea");
var keywords = new Array("var", "if");
function checkHighlight(){
var codeInput1 = codeInput[0].value;
if(codeInput1 === keywords[0]){
 keywords[0].indexOf(codeInput1).className = "JSfunctions";
}
}
</script>
<style type="text/css" runat="server">
    #JScodeinputbox{font-family:Arial;}
  #JScodeoutputbox{}
    .JSfunctions{color:blue;}
</style>
 </head>
 <body>
 <form id="form1" runat="server">
 <div>
 <textarea id="JScodeinputbox" wrap="logical" rows="30" cols="70" onkeyup="checkHighlight();"></textarea>
</div>
</form>
</body>
</html>

在jQuery中:

 $("html").append(" <textarea id='text' type='text'> </textarea> "); //create an input box $("html").append(" <input id='submit' type='submit'> "); //create a submit button $("input").css("border", "1px solid black"); $("#submit").click(function() { var unit = $("#text").val(); if (unit == "var") { $("#text").css("border", "2px solid blue"); $("#text").css("background", "blue"); } else { $("#text").css("border", "1px solid black"); $("#text").css("background", "white"); } }); $(document).keydown(function(e) { if (e.keyCode == 13 || e.keyCode == 32) { var unit = $("#text").val(); if (unit == "var") { $("#text").css("border", "2px solid blue"); $("#text").css("background", "blue"); } else { $("#text").css("border", "1px solid black"); $("#text").css("background", "white"); } } }); $("html").append("(You can also press enter or space)"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

暂无
暂无

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

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