我試圖建立一個類似於Twitter的登錄表單,但我有一個問題。點擊「登錄」後,會出現表單。當再次單擊「登錄」時,表單將消失。這很好,但我希望表單在用戶在表單外單擊時也會消失。jQuery嘰嘰喳喳樣式登錄
<!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>jQuery Testing</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function() {
$('.signIn').click(function() {
$(this).toggleClass('highlight');
$('form').toggleClass('show');
});
});
</script>
<style type="text/css">
#container {
margin: 0 auto;
width: 920px;
border: #ccc solid 1px;
padding: 20px;
overflow: auto;
background: #ebebeb;
}
.signIn {
float: right;
padding: 5px 5px 0;
}
form {
background: #0000ff;
padding: 10px;
width: 200px;
float: right;
clear: both;
position: absolute;
margin-left: 700px;
margin-top: 25px;
display: none;
}
label, input {
display: block;
}
.highlight {
background: #0000ff;
color: #fff;
}
.show {
display: block;
}
</style>
</head>
<body>
<div id="container">
<div class="right">
<a href="#" class="signIn">Sign In</a>
<form>
<label>Username</label>
<input type="text" />
<label>Password</label>
<input type="text" />
</form>
</div>
</div>
</body>
</html>
這個想法很接近,但現在如果點擊任何東西,包括表單本身,表單就會關閉。此外,如果單擊登錄按鈕,表單將不會關閉,因爲該按鈕保留其焦點。儘管如此,我會進一步研究它。我有一種感覺會有用處。 – 2010-10-15 21:46:41