区分 jQuery UI CSS 中的复选框、按钮和单选按钮
我正在为 jQuery UI 编写自己的 CSS。
基本上,我需要为单选按钮、复选框和常规按钮提供不同的样式。
这里的问题是,添加到假小部件的 jQuery 类没有指定它是什么类型的按钮。
例如,假设我们有以下代码:
<input type="checkbox" name="location[]" id="location1" value="Somewhere" />
<label for="location1">Somewhere</label>
<script type="text/javascript">
$(document).ready(function(){
jQuery('input').button();
});
</script>
jQuery 将这两个元素转换为:
<input type="checkbox" name="location[]" id="location1" value="Somewhere"
class="ui-helper-hidden-accessible">
<label for="location1" aria-pressed="false" role="button" aria-disabled="false"
class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">
<span class="ui-button-text">Somewhere</span>
</label>
如您所见,修改后的标签和生成的范围没有任何类告诉我它是一个复选框(或单选按钮)。
理想的解决方案希望基于一些我没有注意到的类。第二个最佳解决方案是对 jQuery UI 进行猴子修补,为这些输入添加更好的类。最终可能的解决方案是破解 jQuery 文件......尽管我真的想避免这种情况。
请注意,任何形式的粉丝主义、非建设性评论和类似答案都不会被容忍。问题是 jQuery UI,我不想听到 mootools、extjs 或您想到的任何流行的 l33t 库。
I'm writing my own CSS for jQuery UI.
Basically, I need to have different styles for radio buttons, checkboxes and regular buttons.
The problem here is that jQuery's classes add to the fake widget do not specify what kind of button it is.
For example, let's say we have this code:
<input type="checkbox" name="location[]" id="location1" value="Somewhere" />
<label for="location1">Somewhere</label>
<script type="text/javascript">
$(document).ready(function(){
jQuery('input').button();
});
</script>
jQuery transforms those two elements into:
<input type="checkbox" name="location[]" id="location1" value="Somewhere"
class="ui-helper-hidden-accessible">
<label for="location1" aria-pressed="false" role="button" aria-disabled="false"
class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">
<span class="ui-button-text">Somewhere</span>
</label>
As you can see, the modified label and generated span do not have any class the tells me it is a checkbox (or radio/button).
The ideal solution would be based on hopefully some class I didn't notice. The second best solution would be to monkey patch jQuery UI to add a better class to those inputs. The final possible solution would be to hack jQuery files...though I really want to avoid this.
Be warned that any kind of fanboyism, nonconstructive comments and similar answers will not be tolerated. The problem is jQuery UI, I don't want to hear about mootools, extjs or whatever trendy l33t library comes up to your mind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以添加
$('input:checkbox').addClass('ui-checkbox')
将ui-checkbox
类添加到输入以及类似的内容:添加复选框的所有标签的
ui-checkbox-label
类you can add
$('input:checkbox').addClass('ui-checkbox')
to addui-checkbox
class to the input and something like that :to add the
ui-checkbox-label
class to all label of your checkbox这是我带来的:
http://jsfiddle.net/fWPK6/
我们使用 jQuery ui create 方法在创建按钮时查找输入类型属性,然后将其添加到标签中。我必须指出一件非常重要的事情,每个输入字段都必须有 id 并且相应的标签必须有正确的 for 属性。
如果您需要对类进行更多自定义,只需修改 addClass(type) 即可。
Here what i came with:
http://jsfiddle.net/fWPK6/
We use jQuery ui create method when creating button to find input type attribute and then add it to the label. I have to point one very important thing, every input field must have id and corresponding label must have correct for attribute.
If you need more customization with class, just tinker with addClass(type).