Knockoutjs 绑定鼠标悬停或 Jquery

发布于 2025-01-03 22:46:52 字数 255 浏览 1 评论 0原文

我在 jquery 中有这段代码。我该如何用绑定将其写在淘汰赛中。 或者把它放在 jquery 中更好。有没有一种聪明的方法来知道何时将其写入绑定或 jquery 中?这是更多的视图,所以也许它应该在 Jquery 中?

$("body").on("mouseover mouseout", '.hoverItem', function () {
    $(this).toggleClass('k-state-selected');
});

I have this code in jquery. How would I write this in knockout with binding.
Or is it better to have it in jquery. Is there a smart way to know when to write it in binding or in jquery? This is more viewstuff so maybe it should be in Jquery ?

$("body").on("mouseover mouseout", '.hoverItem', function () {
    $(this).toggleClass('k-state-selected');
});

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

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

发布评论

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

评论(1

萝莉病 2025-01-10 22:46:52

如果此类的切换实际上并不需要连接到视图模型中的数据,那么您确实没有理由不能执行现在正在执行的操作。

以下是如何使用默认绑定执行此操作的示例:

view:

<ul data-bind="foreach: items">
    <li data-bind="text: name, event: { mouseover: toggle, mouseout: toggle }, css: { hover: selected }"></li>
</ul>

视图模型代码:

var Item = function(name) {
    this.name = ko.observable(name);
    this.selected = ko.observable(false);
    this.toggle = function() {
       this.selected(!this.selected());   
    }
};

var viewModel = {
    items: ko.observableArray([
        new Item("one"),
        new Item("two"),
        new Item("three")
    ])
};

ko.applyBindings(viewModel);

使用自定义绑定,您甚至可以将其简化为:

<ul data-bind="foreach: items">
    <li data-bind="text: name, hoverToggle: 'hover'"></li>
</ul>

view model:

ko.bindingHandlers.hoverToggle = {
    update: function(element, valueAccessor) {
       var css = valueAccessor();

        ko.utils.registerEventHandler(element, "mouseover", function() {
            ko.utils.toggleDomNodeCssClass(element, ko.utils.unwrapObservable(css), true);
        });  

        ko.utils.registerEventHandler(element, "mouseout", function() {
            ko.utils.toggleDomNodeCssClass(element, ko.utils.unwrapObservable(css), false);
        });      
    } 
};

var Item = function(name) {
    this.name = ko.observable(name);
};

var viewModel = {
    items: ko.observableArray([
        new Item("one"),
        new Item("two"),
        new Item("three")
    ])
};

ko.applyBindings(viewModel);

您甚至可以变得更复杂,并让它以委托方式工作,例如 < code>on 通过将绑定置于更高级别并基于选择器应用切换。

就经验而言,我认为当功能不需要视图模型中的数据时,开发人员是否想要使用连接代码或以声明方式执行它取决于开发人员。在您的情况下,可能足以坚持使用 on 直到它需要来自视图模型的数据。

If the toggling of this class doesn't really need to connect to the data in your view model, then there is really no reason that you couldn't do what you are doing now.

Here is a sample of how to do it with the default bindings:

view:

<ul data-bind="foreach: items">
    <li data-bind="text: name, event: { mouseover: toggle, mouseout: toggle }, css: { hover: selected }"></li>
</ul>

view model code:

var Item = function(name) {
    this.name = ko.observable(name);
    this.selected = ko.observable(false);
    this.toggle = function() {
       this.selected(!this.selected());   
    }
};

var viewModel = {
    items: ko.observableArray([
        new Item("one"),
        new Item("two"),
        new Item("three")
    ])
};

ko.applyBindings(viewModel);

With custom bindings you could even reduce it down to:

<ul data-bind="foreach: items">
    <li data-bind="text: name, hoverToggle: 'hover'"></li>
</ul>

view model:

ko.bindingHandlers.hoverToggle = {
    update: function(element, valueAccessor) {
       var css = valueAccessor();

        ko.utils.registerEventHandler(element, "mouseover", function() {
            ko.utils.toggleDomNodeCssClass(element, ko.utils.unwrapObservable(css), true);
        });  

        ko.utils.registerEventHandler(element, "mouseout", function() {
            ko.utils.toggleDomNodeCssClass(element, ko.utils.unwrapObservable(css), false);
        });      
    } 
};

var Item = function(name) {
    this.name = ko.observable(name);
};

var viewModel = {
    items: ko.observableArray([
        new Item("one"),
        new Item("two"),
        new Item("three")
    ])
};

ko.applyBindings(viewModel);

You could even get more sophisticated and have it work in a delegated fashion like on by putting the binding at a higher level and applying the toggle based on a selector.

As far as a rule of thumb, I think that it is up to the developer whether they want to use wire up code or do it declaratively when the functionality does not require data from the view model. In your case, probably good enough to stick with on until it requires data from your view model.

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