如何在Jquery中减少这一行
How to reduce this lines in Jquery?
我有很多行这样的代码,如何减少这些行。也请解释你的代码。如果有任何简单的方法写这样的逻辑代码,请张贴链接。
$(function () {
$("#menu1").click(function () {
$(this).css({"background-color": "rgba(255, 255, 255, 0.4)"});
$("#menu2").css({"background-color": "transparent"});
$("#menu3").css({"background-color": "transparent"});
$("#menu4").css({"background-color": "transparent"});
$("#menu5").css({"background-color": "transparent"});
});
$("#menu2").click(function () {
$(this).css({"background-color": "rgba(255, 255, 255, 0.4)"});
$("#menu1").css({"background-color": "transparent"});
$("#menu3").css({"background-color": "transparent"});
$("#menu4").css({"background-color": "transparent"});
$("#menu5").css({"background-color": "transparent"});
});
$("#menu3").click(function () {
$(this).css({"background-color": "rgba(255, 255, 255, 0.4)"});
$("#menu1").css({"background-color": "transparent"});
$("#menu2").css({"background-color": "transparent"});
$("#menu4").css({"background-color": "transparent"});
$("#menu5").css({"background-color": "transparent"});
});
});
使用jquery Attribute starts with selector
和.not()
。试试这个:
$(function(){
$("[id^=menu]").click(function(){
$(this).css({"background-color":"rgba(255, 255, 255, 0.4)"});
$("[id^=menu]").not(this).css({"background-color":"transparent"});
});
});
给所有的菜单一个共同的类,并这样做
$('.menu').on('click',function(){
$('.menu').css({"background-color":"transparent"});
$(this).css({"background-color":"rgba(255, 255, 255, 0.4)"});
});
相关文章:
- 动态插入的表:JQuery未检测到最后一行
- Jquery幻灯片以一行左侧的图像开始,但我希望它从右端开始
- 如何确保JQuery的一行在另一行之前运行
- 使用Ajax和JQuery从SQL数据库中添加和删除一行
- 如何防止在选定的jQuery多选时一行重复条目
- 如何使用jquery复制文本区域中的每一行文本
- jquery插件 - 已选择 - 多项选择不在一行中列出,而是列在一列中
- 如何使用 jQuery 在文本区域中的每一行添加文本
- 使用jQuery删除表行,但禁止删除第一行
- 如果最后一行已经显示,如何防止jQuery加载更多停止函数
- 通过jQuery在文本区域占位符内新建一行
- 如何在jquery菜单中的li项下添加一行
- 试图美化一行jQuery
- 如何在数据表中复制一行(jQuery DataTables插件)
- 用一行(jQuery)连接不同框架中的2个元素
- 在一行jquery中对输入和选择进行操作
- 获取一行jQuery上的最后一个li
- 在表前面加上第一行 jQuery
- 如何从一行jQuery中获取数据
- 在前一行代码完成后运行一行jQuery