CSS分別設定Input樣式(按input類型)

來源:互聯網
上載者:User

標籤:

當你看到<input>這個html標籤的時候,你會想到什嗎?一個文字框?一個按鈕?一個單選框?一個複選框?……對,對,對,它們都對。也許你可能想不到,這個小小的input竟然可以創造出10個不同的東西,下面是個列表,看看,哪些是你沒有想到的:

<input type="text" /> 文字框 
<input type="password" /> 密碼框 
<input type="submit" /> 提交按鈕 
<input type="reset" /> 重設按鈕 
<input type="radio" /> 單選框 
<input type="checkbox" /> 複選框 
<input type="button" /> 普通按鈕 
<input type="file" /> 檔案選擇控制項 
<input type="hidden" /> 隱藏框 
<input type="image" /> 圖片按鈕

所以你可能會說,input真是一個偉大的東西,竟然這麼有“搞頭”,但是當你真正在項目中試圖給不同的控制項設定不同的樣式時,你就會發現,input真的可以把“你的頭搞大”。我不知道為什麼當初要給input賦予那麼多身份,但是,他的“N重身份”給網站設計者的確帶來了不少的麻煩。好在,勞動人民是偉大的,解決問題的辦法還是有滴~,雖然它們都有各自致命的缺點 Orz… 解放方法大致歸納一下,列表如下(小弟才疏,錯誤遺漏難免,還請各位高人指點):

1.用css的expression判斷運算式

2.用css中的type選取器

3.用javascript指令碼實現

4.如果你用Microsoft Visual Studio 2005 或者後續版本開發項目,恭喜,你還可以使用skin。

下面就來講解一下各個辦法的詳細實現和它們的優缺點。

1:用css的expression判斷運算式

實現代碼參考:

<!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>

    <title> diffInput2 </title>

    <meta name="Author" content="JustinYoung"/>

    <meta name="Keywords" content=""/>

    <meta name="Description" content=""/>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <style type="text/css">

    input

    {

    padding: 0px; line-height: 1.8; color: rgb(0, 0, 255);">expression(this.type=="text"?‘#FFC‘:‘‘);

    }

    </style>

</head>

<body>

<dl>

<dt>This is normal textbox:<dd><input type="text" name="">

<dt>This is normal button:<dd><input type="button" value="i‘m button">

</dl>

</body>

</html>

優點:簡單,輕量級

缺點:expression判斷運算式FireFox是不支援的。致命的是只能區分出一個(例如例子中就只能區分出text文字框),不要試圖設定多個,下面的會將上面的覆蓋掉 Orz…

★★★★★★★★★★★★★★★★★★★★★★★★★★★

另一種方法:

input{
    zoom: expression(function(ele){(ele.className)?ele.className+=" "+ele.type:ele.className=ele.type; ele.style.zoom = "1";}(this));
}


兩點:

1、將 input 的屬性取出來,賦給 className。

2、對於 expression,這裡使用一個無關緊要的屬性(此處是zoom)來觸發,處理完需要做的事情之後,再將此屬性覆蓋掉以解決 expression 不斷執行的效率問題。


<!--[if lt IE 7]>

<style type="text/css" media="screen">
input{ 
zoom: expression(function(ele){(ele.className)?ele.className+=" "+ele.type:ele.className=ele.type; ele.style.zoom = "1";}(this));
}
input.text{
border: 1px solid; border-color: #CCC #EEE #EEE #CCC;
background: #F5F5F5;
}
input.password{
border: 1px solid; border-color: #CCC #EEE #EEE #CCC;
color: #000; background: #F5F5F5;
width: 50px;
}
input.button{
border: 1px solid; border-color: #EEE #CCC #CCC #EEE;
color: #000; font-weight: bold; background: #F5F5F5;
}
input.reset{
border: 1px solid; border-color: #EEE #CCC #CCC #EEE;
color: #666; background: #F5F5F5;
}
</style>
<![endif]-->

<style type="text/css" media="all">
input[type="text"]{
border: 1px solid; border-color: #CCC #EEE #EEE #CCC;
background: #F5F5F5;
}
input[type="password"]{
border: 1px solid; border-color: #CCC #EEE #EEE #CCC;
color: #000; background: #F5F5F5;
width: 50px;
}
input[type="button"]{
border: 1px solid; border-color: #EEE #CCC #CCC #EEE;
color: #000; font-weight: bold; background: #F5F5F5;
}
input[type="reset"]{
border: 1px solid; border-color: #EEE #CCC #CCC #EEE;
color: #666; background: #F5F5F5;
}
</style>
</head>
<body>
<input type="text" name="xx" />
<input type="password" name="yy" />
<input type="checkbox" name="oo" />
<input type="radio" name="pp" />
<input type="button" name="qq" value="button" />
<input type="reset" name="oo" value="reset" />
</body>
</html>

 

★★★★★★★★★★★★★★★★★★★★★★★★★★★

 

2:用css中的type選取器

實現參考代碼:

<!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>

    <title> diffInput2 </title>

    <meta name="Author" content="JustinYoung"/>

    <meta name="Keywords" content=""/>

    <meta name="Description" content=""/>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <style type="text/css">

    input[type="text"]

    {

   

    }

   

    input[type="password"]

    {

    background-image:url(BG.gif);

    }

   

    input[type="submit"]

    {

   

    color:white;

    }

   

    input[type="reset"]

    {

   

    color:white;

    }

   

   input[type="radio"]

    {

    /*In FF,Some radio style like background-color not been supported*/

    margin:10px;

    }

   

    input[type="checkbox"]

    {

    /*In FF,Some checkbox style like background-color not been supported*/

    margin:10px;

    }

   

    input[type="button"]

    {

   

    }

    </style>

</head>

<body>

<dl>

<dt>This is normal textbox:<dd><input type="text" name="">

<dt>This is password textbox:<dd><input type="password" name="">

<dt>This is submit button:<dd><input type="submit">

<dt>This is reset button:<dd><input type="reset">

<dt>This is radio:<dd><input type="radio" name="ground1"> <input type="radio" name="ground1">

<dt>This is checkbox:<dd><input type="checkbox" name="ground2"> <input type="checkbox" name="ground2">

<dt>This is normal button:<dd><input type="button" value="i‘m button">

</dl>

</body>

</html>

優點:簡單,明了,可以分區出各個input控制項形態。

缺點:type選取器,IE6之前的對web標準支援的不太好的瀏覽器不能支援(致命呀 Orz…)

 

3:用javascript指令碼實現

實現參考代碼:

前台html代碼:

<!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>

    <title> diffInput </title>

    <meta name="Author" content="JustinYoung">

    <meta name="Keywords" content="">

    <meta name="Description" content="">

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >

    <style type="text/css">

    input{behavior:url(‘css.htc‘);}

    </style>

</head>

<body>

<dl>

<dt>This is normal textbox:<dd><input type="text" name="">

<dt>This is password textbox:<dd><input type="password" name="">

<dt>This is submit button:<dd><input type="submit">

<dt>This is reset button:<dd><input type="reset">

<dt>This is radio:<dd><input type="radio" name="ground1"> <input type="radio" name="ground1">

<dt>This is checkbox:<dd><input type="checkbox" name="ground2"> <input type="checkbox" name="ground2">

<dt>This is normal button:<dd><input type="button" value="i‘m button">

</dl>

</body>

</html>

Css.htc代碼:

<script language=javascript>

switch(type)

{

    case ‘text‘:

    style.backgroundColor="red";

    break;

    case ‘password‘:

    style.backgroundImage="url(BG.gif)";

    break;

    case ‘submit‘:

    style.backgroundColor="blue";

    style.color="white";

    break;

    case ‘reset‘:

    style.backgroundColor="navy";

    style.color="white";

    break;

    case ‘radio‘:

    style.backgroundColor="hotpink";

    break;

    case ‘checkbox‘:

    style.backgroundColor="green";

    break;

    case ‘button‘:

    style.backgroundColor="lightblue";

    break;

    default: ;//others use default style.

}

</script>

優點:可以分區出各個input控制項形態。多種技術的混合使用,滿足“我是高手”的虛榮心。

缺點:技術牽扯麵教廣,因為用js後期處理,所以在js沒有起作用之前,各個input還是原始狀態,然後突然“變帥”會讓你的頁面很奇怪。較致命的是FireFox不支援 Orz…

4:Microsoft Visual Studio 2005中使用skin。

Skin檔案參考代碼:

<%--Style for common TextBox--%>

<asp:TextBox runat="server" style="</asp:TextBox>

<asp:Button runat="server" style=”background-color:red”></asp:Button>


注意裡面的樣式是用style加上的,而不是用cssClass,道理很簡單,如果用cssClass,前面的再用cssClass就會覆蓋這個cssClass。導致失敗。當然,skin不能單獨使用,還要配合css樣式表。

優點:可以分區出各個控制項形態(注意:skin只能對伺服器端控制項使用,所以現在已經不是單純的input標籤了,雖然這些伺服器端控制項“打到”前台的時候仍然是input控制項)。除了css,又被分離一層,使得樣式的設定能有更好的定製性。其他優點(參考skin的優點)。

CSS分別設定Input樣式(按input類型)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.