javascript - Hovercard 在带绑定(bind)的 knockoutJS 中不显示

标签 javascript jquery css knockout.js

我得到了这张悬停卡片,我想在子菜单中显示它。它在标题中工作,但不知何故,当在子菜单“with:$root.chosenMenu”-bindings 中时,悬停效果不会启动。

这是我的代码:

HTML:

<div>
    <span class="previewCard">    
        <label class="hovercardName" data-bind="text: displayName"></label>
        <span class="hovercardDetails">     
            <span data-bind="text: miniBio"></span>.<br/>
            <a data-bind="attr: { href: webpage, title: webpage }, text: webpage">My blog</a>
        </span>
    </span> 
    <br/><br/>
    <div class="wysClear wysLeft buttonRow">
        <a href="#pid1" data-bind="click: function(){$root.chosenMenu('a');return false;}">Panel A</a>
        <a href="#pid2" data-bind="click: function(){$root.chosenMenu('b');return false;}">Panel B</a>
    </div>
</div>
<hr/>
<div data-bind="with: $root.chosenMenu">
    <div id="pid1" data-bind="visible: $root.chosenMenu() === 'a'">
        panel A: <br/><br/>
        <span class="previewCard">    
            <label class="hovercardName" data-bind="text: $root.displayName"></label>
            <span class="hovercardDetails">     
                <span data-bind="text: $root.miniBio"></span>.<br/>
                <a data-bind="attr: { href: $root.webpage, title: $root.webpage }, text: $root.webpage">My blog</a>
            </span>
        </span> 
    </div>
    <div id="pid2" data-bind="visible: $root.chosenMenu() === 'b'">
        panel B: <br/><br/>
        <span class="previewCard">    
            <label class="hovercardName" data-bind="text: $root.displayName"></label>
            <span class="hovercardDetails">     
                <span data-bind="text: $root.miniBio"></span>.<br/>
                <a data-bind="attr: { href: $root.webpage, title: $root.webpage }, text: $root.webpage">My blog</a>
            </span>
        </span> 
    </div>
</div>

Javascript:

viewModel = function(){
    var self = this;

    self.chosenMenu = ko.observable();
    self.displayName = ko.observable("Asle G");
    self.miniBio = ko.observable("Everywhere we go - there you are!");
    self.webpage = ko.observable("http://blog.a-changing.com")
};

ko.applyBindings( new viewModel() );

// hovercard
$(".previewCard").hover(function() {
    $(this).find(".hovercardDetails").stop(true, true).fadeIn();
}, function() {
    $(this).find(".hovercardDetails").stop(true, true).fadeOut();
});

CSS:

.previewCard {
    position:relative;            
}
.hovercardName {    
    font-weight:bold;    
    position:relative;    
    z-index:100; /*greater than details, to still appear in card*/
}
.hovercardDetails {            
    background:#fff ;    
    border:solid 1px #ddd;      
    position:absolute ;
    width:300px;
    left:-10px;
    top:-10px;
    z-index:50; /*less than name*/
    padding:2em 10px 10px; /*leave enough padding on top for the name*/   
    display:none;
}

fiddle :http://jsfiddle.net/AsleG/jb6b61oh/

最佳答案

您有一个经典的 jQuery 问题:事件处理程序仅附加到当时存在的元素。

这会将 hover (mouseenter/mouseleave) 事件处理程序附加到唯一的 .previewCard 上:

$(".previewCard").hover(function() {
    $(this).find(".hovercardDetails").stop(true, true).fadeIn();
}, function() {
    $(this).find(".hovercardDetails").stop(true, true).fadeOut();
});

但是如果 knockout 动态地创建一些,它们将没有任何事件处理程序。因此,您必须将事件处理委托(delegate)给层次结构中更高的节点。

$(document).on("mouseenter", ".previewCard", function() {
    $(this).find(".hovercardDetails").stop(true, true).fadeIn();
}).on("mouseleave", ".previewCard", function() {
    $(this).find(".hovercardDetails").stop(true, true).fadeOut();
});

这仍然不是解决问题的干净方法。为什么不创建一个 hovercard 绑定(bind)处理程序并完全摆脱 View 模型中的 jQuery 代码?

ko.bindingHandlers.hovercard = {
    init: function (element) {
        $(element).hover(function() {
            $(this).find(".hovercardDetails").stop(true, true).fadeIn();
        }, function() {
            $(this).find(".hovercardDetails").stop(true, true).fadeOut();
        });
    }
};

用作:

<span class="previewCard" data-bind="hovercard: true">

http://jsfiddle.net/jb6b61oh/6/

关于javascript - Hovercard 在带绑定(bind)的 knockoutJS 中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28928062/

相关文章:

javascript - Angular JS : Toggle Only One Item at a Time

javascript - KnockoutJS - 绑定(bind)不会使用映射插件更新

javascript - jQuery 序列化问题

css - flex 圆形英雄按钮

javascript - 将按钮的 Action 与其 div 的 Action 隔离

css - 如何使此元素在此代码中按应有的方式显示?

javascript - 从字符串中删除连字符的最快方法

javascript - 使用正则表达式转义单词开头和冒号之间的特殊字符

javascript - 在 ExtJS 之后加载 jQuery

javascript - 为什么这个最简单的 jQuery 在 iframe 代码中隐藏元素即使在同源上也不起作用?