我试图使文本字段更改其颜色,但它似乎不起作用,这是代码:
<html>
<head>
  <title>   
  This is a title
  </title>
<script type="text/javascript">
    function changeColor()
    {
    alert("bla")
    document.getElemenyById("text1").style.background-color:red;
    }
</script>
</head>
<body>
<form>
<input id="text1" type="text" onkeypress="changeColor()">
</form>
</body>
</html>发布于 2013-09-07 13:11:56
1)从JavaScript访问的大多数CSS属性都需要替换所有的"-“并使用rpn样式,即。
背景色变成backgroundColor,
边界崩溃变成borderCollapse等等。
2)在JavaScript中操作样式时,常常需要同时更新多个属性。一个好的、优雅的方法是使用JavaScript "with“语句:
with(document.getElementById("text1").style) {
    backgroundColor = 'red' ;
    fontWeight = 'bold' ;
    // etc... 
} 3) jQuery方法也很简单:
$('#text1').css({
    "background-color": "red", 
    "font-weight": "bold"
}) ;http://api.jquery.com/css/
https://stackoverflow.com/questions/18673500
复制相似问题