码迷,mamicode.com
首页 > 其他好文 > 详细

addclass无效

时间:2016-05-12 16:07:01      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

网上遇到addclass无效的问题有很多,每个人的原因和解决方法都不一样

我遇到的问题是这样的

html代码如下:

<div class="cd-user-modal-container">
			<ul class="cd-switcher">
				<li><a  href="#0">用户登录</a></li>
				<li><a href="#0">注册新用户</a></li>
			</ul>

			<div id="cd-login"> <!-- 登录表单 -->
				<form class="cd-form">
					<p class="fieldset">
						<label class="image-replace cd-username" for="signin-username">用户名</label>
						<input class="full-width has-padding has-border" id="signin-username" type="text" placeholder="输入用户名">
					</p>

					<p class="fieldset">
						<label class="image-replace cd-password" for="signin-password">密码</label>
						<input class="full-width has-padding has-border" id="signin-password" type="text"  placeholder="输入密码">
					</p>

					<p class="fieldset">
						<input type="checkbox" id="remember-me" checked>
						<label for="remember-me">记住登录状态</label>
					</p>

					<p class="fieldset">
						<input class="full-width2" type="submit" value="登 录">
					</p>
				</form>
			</div>

			<div id="cd-signup"> <!-- 注册表单 -->
				<form class="cd-form">
					<p class="fieldset">
						<label class="image-replace cd-username" for="signup-username">用户名</label>
						<input class="full-width has-padding has-border" id="signup-username" type="text" placeholder="输入用户名">
					</p>

					<p class="fieldset">
						<label class="image-replace cd-email" for="signup-email">邮箱</label>
						<input class="full-width has-padding has-border" id="signup-email" type="email" placeholder="输入mail">
					</p>

					<p class="fieldset">
						<label class="image-replace cd-password" for="signup-password">密码</label>
						<input class="full-width has-padding has-border" id="signup-password" type="text"  placeholder="输入密码">
					</p>

					<p class="fieldset">
						<input type="checkbox" id="accept-terms">
						<label for="accept-terms">我已阅读并同意 <a href="#0">用户协议</a></label>
					</p>

					<p class="fieldset">
						<input class="full-width2" type="submit" value="注册新用户">
					</p>
				</form>
			</div>

			<a href="#0" class="cd-close-form">关闭</a>
		</div>
	

 css代码如下:

.cd-user-modal-container .cd-switcher a {
	display: block;
	width: 100%;
	height: 50px;
	line-height: 50px;
	background: #d2d8d8;
	color: #809191;
	 
}
.cd-user-modal-container .cd-switcher a.selected {
  background: #FFF;
  color: #505260;
  
}


#cd-login, #cd-signup, #cd-reset-password {
  display: none;
}

#cd-login.is-selected, #cd-signup.is-selected, #cd-reset-password.is-selected {
  display: block;
}


js代码如下:

	function login_selected(){
		$tab_login.addClass('selected');
		$tab_signup.removeClass('selected');
		$form_login.addClass('is-selected');
		$form_signup.removeClass('is-selected');
		$form_forgot_password.removeClass('is-selected');
		
	}

	function signup_selected(){
		$tab_login.removeClass('selected');
		$tab_signup.addClass('selected');
		$form_login.removeClass('is-selected');
		$form_signup.addClass('is-selected');
		$form_forgot_password.removeClass('is-selected');
		
	}


如果按照上述代码,运行结果是正确的

点击“登录”和点击“注册‘样式会改变

技术分享          技术分享


但源代码之前给出的js代码是这样的:

	function login_selected(){
		
		$form_login.addClass('is-selected');
		$form_signup.removeClass('is-selected');
		$form_forgot_password.removeClass('is-selected');
		$tab_login.addClass('selected');
		$tab_signup.removeClass('selected');
		
	}

	function signup_selected(){
	
		$form_login.removeClass('is-selected');
		$form_signup.addClass('is-selected');
		$form_forgot_password.removeClass('is-selected');
		$tab_login.removeClass('selected');
		$tab_signup.addClass('selected');
	}

这也就是$tab_login和$tab_signup的addclass()的顺序问题,如果顺序在后面,他们的addclass在后面,就会无效,结果就会变成下面这样,至今还不知道什么原因。

技术分享



addclass无效

标签:

原文地址:http://blog.csdn.net/qq_26222859/article/details/51365448

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!