javascript - jQuery 添加一个类但不会删除一个

标签 javascript jquery html css

当您单击 .extra anchor 时,它会添加“extra-active”类并删除“extra”类,但是当您单击 extra-active anchor 时,它拒绝删除 extra-active 类并将其替换为 extra .

这是 HTML

	$("a.extra").click(function(e){
		$(this).addClass("extra-active").removeClass("extra");
		e.preventDefault();
	})

	$("a.extra-active").click(function(e){
		$(this).removeClass("extra-active").addClass("extra");
		e.preventDefault();
	})
.extra{
	display:inline-block;
	height: 128px;
	width: 128px;

	background-color:grey;
	padding:12px;
	border-radius:8px;
}

.extra-group{
	p {
		text-align:center;
	}
}


.extra:hover{
	background-color:blue;
}

.extra-active{
	display:inline-block;
	height: 128px;
	width: 128px;

	background-color:red !important;
	padding:12px;
	border-radius:8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span style="display:inline-block;">
	<span class="extra-group">
		<a class="extra" href="" title = "An hour of Ironing" id="extra-ironing" style = "background-image: url('{{ url_for('static', filename='book-icons/iron-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
		<p>An Hours Ironing</p>
	</span>
	<span class="extra-group">
		<a class="extra" href="" title = "Inside the Fridge" id="extra-fridge"  style = "background-image: url('{{ url_for('static', filename='book-icons/fridge-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
		<p>Inside the Fridge</p>
	</span>
	<span class="extra-group">
		<a class="extra" href="" title = "Inside the Oven" id="extra-oven"  style = "background-image: url('{{ url_for('static', filename='book-icons/oven-icon.png') }}'); background-size: 80% 80%;; background-repeat: no-repeat; background-position: center center;"></a>
		<p>Inside the Oven</p>
	</span>
	<span class="extra-group">
		<a class="extra" href="" title = "A load of Washing" id="extra-washing" style = "background-image: url('{{ url_for('static', filename='book-icons/washer-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
		<p>A load of Washing</p>
	</span>
	<span class="extra-group">
		<a class="extra " href="" title = "Interior Windows" id="extra-windows" style = "background-image: url('{{ url_for('static', filename='book-icons/window-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
		<p>Interior Windows</p>
	</span>
</span>

最佳答案

扩展@xandercoded 的贡献,我向主体添加了一个监听器,然后更新了 on 函数的目标。

	$("body").on('click','.extra',function(e){
		$(e.target).addClass("extra-active").removeClass("extra");
		e.preventDefault();
	})

	$("body").on('click','.extra-active',function(e){
		$(e.target).removeClass("extra-active").addClass("extra");
		e.preventDefault();
	})
.extra{
	display:inline-block;
	height: 128px;
	width: 128px;

	background-color:grey;
	padding:12px;
	border-radius:8px;
}

.extra-group{
	p {
		text-align:center;
	}
}


.extra:hover{
	background-color:blue;
}

.extra-active{
	display:inline-block;
	height: 128px;
	width: 128px;

	background-color:red !important;
	padding:12px;
	border-radius:8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span style="display:inline-block;">
	<span class="extra-group">
		<a class="extra" href="" title = "An hour of Ironing" id="extra-ironing" style = "background-image: url('{{ url_for('static', filename='book-icons/iron-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
		<p>An Hours Ironing</p>
	</span>
	<span class="extra-group">
		<a class="extra" href="" title = "Inside the Fridge" id="extra-fridge"  style = "background-image: url('{{ url_for('static', filename='book-icons/fridge-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
		<p>Inside the Fridge</p>
	</span>
	<span class="extra-group">
		<a class="extra" href="" title = "Inside the Oven" id="extra-oven"  style = "background-image: url('{{ url_for('static', filename='book-icons/oven-icon.png') }}'); background-size: 80% 80%;; background-repeat: no-repeat; background-position: center center;"></a>
		<p>Inside the Oven</p>
	</span>
	<span class="extra-group">
		<a class="extra" href="" title = "A load of Washing" id="extra-washing" style = "background-image: url('{{ url_for('static', filename='book-icons/washer-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
		<p>A load of Washing</p>
	</span>
	<span class="extra-group">
		<a class="extra " href="" title = "Interior Windows" id="extra-windows" style = "background-image: url('{{ url_for('static', filename='book-icons/window-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
		<p>Interior Windows</p>
	</span>
</span>

关于javascript - jQuery 添加一个类但不会删除一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31710827/

相关文章:

javascript - 使用原型(prototype)优于在构造函数中创建函数的优势?

javascript - 秒表不工作

javascript - 使用 React 单击另一个表复选框来选择表的所有复选框

android - 移动安卓设备上的 CSS 背景图像全屏并不完美

html - 如何底部对齐 flex 元素内的元素?

javascript - 我是否需要在同构 JavaScript 应用程序中使用动态元标记?

javascript - 如何在页面加载时提交ajax表单

javascript - 主干集合 .'at(index)' 正在返回多个元素

jQuery $ ("&lt;script&gt;&lt;/script&gt;").appendTo ("head");不会出现在生成的源代码中。如何?

jquery - 使用 jQuery 验证文本字段是否为数字