通过数据属性选择jQuery对象 [英] Select jquery object by data attribute

查看:67
本文介绍了通过数据属性选择jQuery对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html片段类似于

My html fragment goes something like

<button data-file="day">Day</button>
<button data-file="night">Night</button> 

并且我正在尝试在Jquery中进行以下操作

and I'm trying to do the following in Jquery

var $button = $('button');
$button.data('file' , 'day').attr('disabled', 'disabled');

因此,使所有按钮成为jQuery对象,然后禁用数据属性为day的按钮

So make jQuery objects of all buttons then disable the button with data attribute day

推荐答案

您需要使用属性选择器,并且需要使用 .prop()设置禁用状态

You need to use the attribute selector and need to use .prop() to set the disabled state

var $button = $('button[data-file="day"]');
$button.prop('disabled' , true);

更新

var $button = $('button');
$button.filter('[data-file="day"]').prop('disabled' , true);

这篇关于通过数据属性选择jQuery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆