knockout js attr绑定以便js获取attributes的值

本文介绍了一种使用JSP和JS技术实现按钮背景颜色动态变化的方法。通过数据绑定,当按钮的数据属性改变时,其背景颜色会根据特定条件自动更新。此方法适用于需要视觉反馈的用户界面设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

JSP、

 <div id="info" data-bind="foreach:itemsA">
     <button name="userBtn" style="width:155px;height:30px;margin-left:8px;margin-top:5px"


             data-bind="style:{backgroundColor:$data.count>0?'#00c1de':''},attr:                                                                                       {count:$data.count},data:$data,click:$root.assignUser,value:userId,text:userName"></button>
 </div>

 

JS、

        function setChecked(){
            var $boxes = $("button[name='userBtn']");
            for(var i=0;i<$boxes.length;i++){
                var id = $boxes[i].value;
                if(checkedIds.indexOf(id,0)!=-1||$boxes[i].attributes.count.value>0){
                    $boxes[i].style.backgroundColor = "#00c1de";
                }else{
                    $boxes[i].style.backgroundColor = "";
                }
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值