js控制所有页面内<input type="text">的样式希望可以实现的效果是 点击页面内的某个<input type="text">的控件时 控件背景颜色改变就是onfocus时  textbox背景颜色改变

解决方案 »

  1.   

    [code=JScript]
      $(document).ready(function() {
        $('input').click(function() {
          this.css('background-color', '#f0f0f0');
        });
        $('input').blur(function() {
          this.css('background-color', '');
        });
      });
    [/code
      

  2.   

    试试这个看
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function fnInitInputFocus()
    {
    var _oInputs = document.getElementsByTagName("input");
    for (var i=0; i<_oInputs.length; i++)
    {
    if (_oInputs[i].type == "text")
    {
    _oInputs[i].onfocus = function()
    {
    this.style.backgroundColor = "#0000ff";
    }
    _oInputs[i].onblur = function()
    {
    this.style.backgroundColor = "";
    }
    }
    }
    }
    function Main()
    {
    fnInitInputFocus();
    }
    </script>
    </head><body onload="Main()">
    <form>
    <input name="tagV1" type="text" /><br />
    <input name="tagV2" type="text" /><br />
    <input name="tagV3" type="text" /><br />
    <input type="submit" value="submit" />
    </form>
    </body>
    </html>
      

  3.   

    在js里面设定样式即可
    document.getElementById("youput").style.backgroundColor = "";
      

  4.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script language="javascript" src="js/jquery-1.3.2.min.js"></script>
    </head>
    <body>
    <input type="text" style="background-color:yellow"/>
    </body>
    </html>
    <script language="javascript">
    $(function()
    {

    $('input[type=text]').focus(textFocus);
    $('input[type=text]').blur(textBlur);
    });
    function textFocus()
    {
    $(this).css('background-color', 'green');
    }
    function textBlur()
    {
    $(this).css('background-color', 'yellow');
    }
    </script>
      

  5.   

    this是dom对象,不是jquery对象,$(this)才是jquery对象,才可以引用css方法
      

  6.   

    新人,来学习学习。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    var oldInput;
    function go()
    {
        try{oldInput.style.backgroundColor = "";}catch(e){}
        var oEle = event.srcElement;
        oldInput = oEle;
        if((oEle.tagName.toLowerCase()=="input") && (oEle.type.toLowerCase()=="text"))
            oEle.style.backgroundColor = "red";
    }
    </script>
    </head><body onClick="go();">
    <form>
        <input name="tagV1" type="text" /><br />
        <input name="tagV2" type="text" /><br />
        <input name="tagV3" type="text" /><br />
        <input type="submit" value="submit" />
    </form>
    </body>
    </html>