解决点击事件让背景变红,在点击背景变正常,并且可以多选的效果
生活随笔
收集整理的這篇文章主要介紹了
解决点击事件让背景变红,在点击背景变正常,并且可以多选的效果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先上效果圖,本人前端入門,用的方法比較笨,歡迎大佬說說其他方法
想實現這種效果常規的加個布爾值,然后判斷布爾值是true,還是false來改變背景顏色肯定不行,如果只是針對一個就可以這樣做,但是用戶不可能只選擇一個吧,所以關鍵在于注冊事件的時機,給那一個div.options中的那一個a標簽注冊事件,而不是div.options下所有的a注冊點擊事件。給所有的a注冊事件必定出現問題。
html代碼
css代碼
/*內容區域右邊樣式開始*/ .content>.content_right{width:100%;height:100%;padding-left:95px;background:#fff; } .content>.content_right>div{display: none; } .content>.content_right>.active{display: block; } .content>.content_right>div>span{position: relative;display: block;width:85%;top:50px;margin:0 auto;border:1px solid rgb(247,247,247); } .content>.content_right h4{position: absolute;margin:0 auto;width:100px;top:-7px;left:22%;background:#fff;font-size: 18px;font-weight: bold;text-align: center; } /*選項按鈕區域樣式開始*/ .content>.content_right .options{margin-top:60px;padding-left:10px; } .content>.content_right .options>a{display: inline-block;width:31.3%;height:50px;font-size: 16px;text-align: center;line-height: 50px;background:rgb(247,247,247);border-radius: 5px;padding:0 10px;margin-top:12px; } .content>.content_right .options>a:last-child{margin-right: 0px; } /*選項按鈕區域樣式結束*/ /*內容區域右邊樣式結束*/js代碼
/*點擊選項變紅*/function red(i,count){var $start = true;$(".content>.content_right .options:nth-of-type("+i+")>a:nth-of-type("+count+")").on("click",function(){if($start == true){$(this).css("color","white").css("background","rgb(195,13,35)");$(this).attr("href","javascript:void(0)");$start = false;}else{$(this).css("color","black").css("background","rgb(247,247,247)");$(this).attr("href","javascript:void(0)");$start = true;} })}$(".content>.content_right .options:nth-of-type(1)>a").each(function(){$i = $(this).index() + 1;red(1,$i);})$(".content>.content_right .options:nth-of-type(2)>a").each(function(){$i = $(this).index() + 1;red(2,$i);})$(".content>.content_right .options:nth-of-type(3)>a").each(function(){$i = $(this).index() + 1;red(3,$i);})$(".content>.content_right .options:nth-of-type(4)>a").each(function(){$i = $(this).index() + 1;red(4,$i);})總結
以上是生活随笔為你收集整理的解决点击事件让背景变红,在点击背景变正常,并且可以多选的效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 商旅结算系统总结
- 下一篇: 地下产链——创建安装包捆绑软件(Bund