简体   繁体   English

如何在小部件中使用jquery-ui复选框?

[英]How do I use jquery-ui checkbox within widget?

I am using a jquery-ui checkbox within a container that has class ui-widget-content on it. 我正在容器上使用jQuery-ui复选框,该容器上具有类ui-widget-content。 This is causing the ".ui-widget-content .ui-state-hover" css rule that defines the background property to override the .ui-icon-check rule that defines the correct background-position for the checkbox image. 这将导致定义背景属性的“ .ui-widget-content .ui-state-hover” css规则覆盖为定义复选框图像的正确背景位置的.ui-icon-check规则。 I believe this is because it is more specific. 我相信这是因为它更具体。

The result is that a checkbox within a widget shows the wrong image. 结果是小部件中的复选框显示错误的图像。 How should I handle this? 我该如何处理?

Here is a jsfiddle example . 这是一个jsfiddle示例 In the second div where I have the ui-widget-content class, you can see the checked image is wrong. 在第二个具有ui-widget-content类的div中,您可以看到检查的图像是错误的。

<div class="ui-widget-content">
<label for="cb3">Test 1</label>
<input type="checkbox" id="cb3" class="toggle"/>
<label for="cb4">Test 2</label>
<input type="checkbox" id="cb4" class="toggle" checked="checked"/>
</div>

Note that I can't change the parent div. 请注意,我无法更改父div。 I am working within a dialog that requires that class. 我在需要该类的对话框中工作。

I am surprised I can't find anybody else complaining about this. 我很惊讶我找不到其他人对此抱怨。 I am not sure what I am missing. 我不确定我缺少什么。

So the .ui-icon-check class is being overwritten by a later style. 因此, .ui-icon-check类将由以后的样式覆盖。 You can just write it back. 您可以将其写回。

One Example: https://jsfiddle.net/Twisty/ewabcy3g/2/ (Fixed for blank) 一个示例: https : //jsfiddle.net/Twisty/ewabcy3g/2/ (固定为空白)

CSS 的CSS

.ui-widget-content label span.ui-icon-check {
  background-position: -64px -144px;
}
.ui-widget-content label span.ui-icon-blank {
  background-position: 16px 16px;
}

Another Example: https://jsfiddle.net/Twisty/ewabcy3g/1/ 另一个示例: https : //jsfiddle.net/Twisty/ewabcy3g/1/

jQuery jQuery的

$('.nonwidget,.ui-widget-content').controlgroup({
  "direction": "vertical"
}).find(".ui-icon-check").css("background-position", "-64px -144px");

Hope that helps. 希望能有所帮助。

Update 更新资料

Found something interesting, still a bit of a hack but it seems to help: 找到了一些有趣的东西,但仍然有点hack,但它似乎有所帮助:

https://jsfiddle.net/Twisty/ewabcy3g/3/ https://jsfiddle.net/Twisty/ewabcy3g/3/

When I removed the checked attribute and set it via jQuery, like so: 当我删除选中的属性并通过jQuery进行设置时,如下所示:

$('.toggle').checkboxradio();
$("#cb4").attr("checked", true);
$('.nonwidget,.ui-widget-content').controlgroup({
  "direction": "vertical"
});

I could remove the CSS hacks. 我可以删除CSS hacks。 I found that setting the CSS positioning back, it was still reading the hover background image and not retaining the proper styling. 我发现将CSS位置放回原处,它仍在读取悬停背景图像,而没有保留正确的样式。

I then tried adding checked="checked" back to the HTML and it got screwed up again. 然后,我尝试将checked="checked"添加回HTML,并再次搞砸了。

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

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