具有 HTML5 自定义数据属性通配符选择的 jQuery 选择器
我尝试使用选择器来获取一个元素,然后在子元素中查找任何包含自定义数据属性中的值的元素。然后,我将对具有该值的这些子元素执行操作,在本示例中,它将它们的背景颜色变为黄色。
例如,采用以下 HTML 和 JS 示例,
<div id="RoleUserMaster">
<div id="RoleUser1">
<label for="Role1">Role:</label>
<select name="Role1" class="RoleSelect">
<option value="1">Role1</option>
<option value="2">Role2</option>
<option value="3">Role3</option>
</select>
<label for="User1">User:</label>
<select name="User1" id="selectMe">
<option value="11" data-roleids="1">Smith</option>
<option value="12" data-roleids="2 4">John</option>
<option value="13" data-roleids="1 3">Richard</option>
</select>
</div>
</div>
$('#selectMe').children('[data-roleids=*"1"]').css('background-color','yellow');
我认为该 jQuery 将首先选择元素,然后查看其子元素,并尝试查找自定义属性 data-roleids 包含字符“1”的任何子元素。您还可以查看 http://jsfiddle.net/7NAhD/2/ 来查看它在行动中。
I'm trying to use a selector to grab an element, then look through the children elements for any that contain a value in a custom data attribute. Then I would perform an action on those children elements with the value, in this example it would turn their background-color to yellow.
For example take the following example of HTML and JS
<div id="RoleUserMaster">
<div id="RoleUser1">
<label for="Role1">Role:</label>
<select name="Role1" class="RoleSelect">
<option value="1">Role1</option>
<option value="2">Role2</option>
<option value="3">Role3</option>
</select>
<label for="User1">User:</label>
<select name="User1" id="selectMe">
<option value="11" data-roleids="1">Smith</option>
<option value="12" data-roleids="2 4">John</option>
<option value="13" data-roleids="1 3">Richard</option>
</select>
</div>
</div>
$('#selectMe').children('[data-roleids=*"1"]').css('background-color','yellow');
I thought this jQuery will first select the element, then it would look at it's children, and try and find any children where custom attribute data-roleids contains a character of '1'. You can also take a look at http://jsfiddle.net/7NAhD/2/ to see it in action.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它适用于此:
请参阅 http://api.jquery.com/attribute-contains-单词选择器/
It works with this :
See http://api.jquery.com/attribute-contains-word-selector/