JavaScript_改變表單元素樣式的那些事;

來源:互聯網
上載者:User

前言:要想改變表單元素樣式,一般都是先在CSS檔案中定義想讓表單元素變成的樣式,然後在需要的地方給該元素的className屬性添加上樣式名稱。注意,兩個樣式名稱間要有空格。樣本如下:

=>CSS檔案

body {
color:#000;
// 黑色;
background-color:#FFF; // 白色;
}

input.invalid {
background-color:#FF9; // 黃色;
border: 2px red inset;
}

label.invalid {
color: #F00; // 紅色;
font-weight: bold;
}

=>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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="css.css" />
<title>HTML與CSS</title>

<script language="javascript">
function btnEvent(btn) {
var name = document.getElementById("userName");
name.focus();
var oldclass = name.className;
console.log(oldclass);// reqd;
oldclass ="invalid " + oldclass;
console.log(oldclass);// invalid reqd;
name.className = oldclass;
}
</script>
</head>

<body>
<form action="#">
<table>
<tr><td>名字:</td><td><input type="text" size="30" id="userName" class="reqd" /></td></tr>
<tr><td>密碼:</td><td><input type="text" size="30" id="passwd1" class="reqd" /></td></tr>
<tr><td>密碼確認:</td><td><input type="text" size="30" id="passwd2" class="reqd passwd1" /></td></tr>
<tr><td></td><td><input type="button" value="改變樣式" onclick="btnEvent(this)"</td></tr>
</table>
</form>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.