简体   繁体   English

jQuery的div单选按钮效果

[英]Jquery for radio button effect in divs

I'm realy new to css and jquery and I need help for my project. 我对CSS和jquery真的很陌生,需要我的项目帮助。

I have 3 buttons: disadvantage - average - advantage and I need to make this buttons to work like radio buttons so if I click on disadvantage this button change background color and other butons lose colors if was clicked on him. 我有3个按钮:劣势-平均-优势,我需要使此按钮像单选按钮一样工作,因此,如果我单击劣势,则此按钮会更改背景颜色,如果单击他,则其他按钮会失去颜色。 and disadvantage button get red backrground color. 和劣势按钮变为红色背景色。

  • If I click on average button, this button must get yellow color and other lose color if was clicked on him before 如果我单击“平均”按钮,那么如果之前单击过此按钮,则该按钮必须变为黄色,而其他颜色则消失

-If I click on average button this button must get green background color and other buttons to lose color. -如果单击平均按钮,则此按钮必须具有绿色背景色,而其他按钮则失去颜色。 so like radio buttons 就像单选按钮

BUt I try to do that based on #ID's 但是我尝试根据#ID

$('#price_quality_adv').css('background-color','#C90');
            $('#reliability_adv').css('background-color','#C90');

http://jsfiddle.net/EC44Z/5/ http://jsfiddle.net/EC44Z/5/

please help. 请帮忙。 sorry for my english THANKS! 对不起我的英语谢谢!

Seems to be a scope issue of some kind, not sure if it is exclusive to jsFiddle environment. 似乎是某种范围的问题,不确定它是否是jsFiddle环境所独有的。 When I make this change, it is fixed (for the advantage button at least) 当我进行此更改时,它是固定的(至少对于优势按钮而言)

setQuality = function(qulaity, type_rating, name)

Fiddle: http://jsfiddle.net/EC44Z/8/ 小提琴: http : //jsfiddle.net/EC44Z/8/

There are still some other issues however, the code is a little complex. 但是,还有其他一些问题,代码有点复杂。 It is better to do something like... 最好做类似...

HTML HTML

<a href="#" id="button_advantage" class="button">advantage</a>

CSS CSS

.button{ /* styles for all buttons */ }
.selected{ /* styles for selected button */ }

JS (jQuery) JS(jQuery)

$("#button_advantage").on('click',function(){
    $(this).addClass('selected')
    $(this).siblings().removeClass('selected')
})

Summary 摘要

The code should be separated, so it is easier to maintain, works well in various environments, and gets good search ranking etc... 代码应该分开,以便于维护,在各种环境下都能很好地工作并获得良好的搜索排名等。

HTML is for content, and should be marked up as such, with appropriate classes to group similar items, and unique id for relevant elements. HTML是用于内容的,因此应进行标记,并使用适当的类将相似的项目分组,并为相关元素提供唯一的ID。

CSS is for style, and all styles should be kept together there where possible. CSS是用于样式的,所有样式应尽可能地保持在一起。

JS is for dynamic functionality, so in this case, adding a class, so that the style changes. JS是用于动态功能的,因此在这种情况下,添加一个类,以便更改样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM