Knockout.js - IE -7 css 类问题

发布于 2024-12-12 02:31:36 字数 289 浏览 0 评论 0原文

这是我遇到的一个有趣的小问题 动态设置 div 数组的类。

我使用 Knockout.js 分配通过“attr”绑定使用的类。

这在我测试过的所有浏览器中都运行良好,除了 IE-7(不担心 IE-6 等)

我有一个 jsfiddle 示例,突出显示 问题在这里

在示例中,静态(顶行)应与底部(ko 生成)匹配 在 IE-7 中,我只看到最广泛的 css 选择器颜色(银色)

Here is a funny little issue i've come across with
dynamically setting the class for a array of divs.

Using Knockout.js i'm assigning the classes used via the 'attr' binding.

This works well in all browsers i've tested except IE-7 (Not worried about IE-6 etc)

I have an jsfiddle example highlighting the issue here

In the example the static (top row) should match the bottom one (ko generated)
In IE-7 i'm just seeing the broadest css selector color (Silver)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乱世争霸 2024-12-19 02:31:36

IE7 要求您设置 className 而不是 class

例如,这适用于 IE7 其他浏览器:http://jsfiddle.net/thirtydot/VVuGh /14/

<div data-bind='attr: { "class": classes, "className": classes }'></div>

然而,对这个 IE7 怪癖的支持最好不要出现在您的 HTML 中。虽然我对这个库一无所知,但无法提出这样的建议,但在 Knockout.js 内部会是一个更好的地方。

IE7 requires that you set className instead of class.

For instance, this works in IE7 and other browsers: http://jsfiddle.net/thirtydot/VVuGh/14/

<div data-bind='attr: { "class": classes, "className": classes }'></div>

However, support for this IE7 quirk should ideally not be in your HTML. Inside knockout.js would be a better place, though I know nothing about the library to be able to make such a recommendation.

无畏 2024-12-19 02:31:36

如果在生成模板时无法确定类名(即它是模型的一部分),您可以创建一个 自定义绑定

init / update 方法的内容只是根据 valueAccessor 返回的内容设置 element 的类名代码>.举一个简单的例子,你可以这样做(使用 jQuery):

ko.bindingHandlers.yourBindingName = {
    init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here

        $(element).addClass(valueAccessor());

    },
    update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        // This will be called once when the binding is first applied to an element,
        // and again whenever the associated observable changes value.
        // Update the DOM element based on the supplied values here.

    }
};

你可以基于敲除构建一个更复杂的绑定 CSS 绑定

If your class name can't be determined when you generate the template (i.e. it is part of your model), you could create a custom binding.

The content of your init / update methods would just set the class name for element based on what is returned by the valueAccessor. For a simple example, you could do (using jQuery):

ko.bindingHandlers.yourBindingName = {
    init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here

        $(element).addClass(valueAccessor());

    },
    update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        // This will be called once when the binding is first applied to an element,
        // and again whenever the associated observable changes value.
        // Update the DOM element based on the supplied values here.

    }
};

You might build a more complex binding based on the knockout css binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文