IE8中的KnockoutJS绑定失败

时间:2012-05-04 20:12:30

标签: javascript internet-explorer-8 knockout.js

以下淘汰赛绑定在真实浏览器中运行良好,但在IE8(兼容模式下的IE9)中失败

<li data-bind="attr: { class: id() == $root.currentItem().id() ? 'active' : '' }"> 

enter image description here

有谁知道为什么,以及我如何解决它?


以下是整个代码,如果有帮助的话:

<div class="toc-outer">
    <ol id="table-of-contents" data-bind="sortable: { data: list, template: 'listTemplate', allowDrop: false, isEnabled: false }" class="toc toc-tree"></ol>
</div>

<script id="listTemplate" type="text/html">
    <li data-bind="attr: { class: id() == $root.currentItem().id() ? 'active' : '' }">
        <div data-bind="attr: { class: 'dropdown' + (id() == $root.currentItem().parentId() ? ' active' : '') }">
            <a href="#" class="toggle">
                <i data-bind="visible:children().length > 0, attr: { class: 'icon-toggle icon-minus hide' + (id() == $root.currentItem().id() ? ' icon-white' : '') }"></i>
            </a>
            <a class="dropdown-toggle menu"
                href="#"
                data-bind="visible:$root.allowEditing"
                data-toggle="dropdown">

                <i data-bind="attr: { class: 'icon-menu icon-list-alt hide' + (id() == $root.currentItem().id() ? ' icon-white' : '') }"></i>
            </a>
            <a class="name" data-bind="attr: {href: hash}">
                <i data-bind="visible: type() == 'settings', attr: { class: 'icon-type icon-settings icon-home' + (id() == $root.currentItem().id() ? ' icon-white' : '') }"></i>
                <i data-bind="visible: type() == 'video', attr: { class: 'icon-type icon-video icon-facetime-video' + (id() == $root.currentItem().id() ? ' icon-white' : '') }"></i>
                <i data-bind="visible: type() == 'data', attr: { class: 'icon-type icon-data icon-signal' + (id() == $root.currentItem().id() ? ' icon-white' : '') }"></i>
                <i data-bind="visible: type() == 'file', attr: { class: 'icon-type icon-file icon-download-alt' + (id() == $root.currentItem().id() ? ' icon-white' : '') }"></i>
                <i data-bind="visible: type() == 'test', attr: { class: 'icon-type icon-test icon-ok-sign' + (id() == $root.currentItem().id() ? ' icon-white' : '') }"></i>
                <span data-bind="text:name"></span>
                <span class="badge draft" data-bind="visible:status() == 'draft'">D</span>
            </a>
            <dl class="dropdown-menu" data-bind="visible:$root.allowEditing">
                <dd><a href="#" data-bind="visible: parentId() !== 0 && !topLevelChild(), click: moveOut" data-toggle="pill" data-target="#item-pane" title="#{TocMoveOut}" class="moveItemOut">#{TocMoveOut}</a></dd>
                <dd><a href="#" data-bind="visible: parentId() !== 0 && !firstSibling(), click: moveIn" data-toggle="pill" data-target="#item-pane" title="#{TocMoveIn}" class="moveItemIn">#{TocMoveIn}</a></dd>
                <dd data-bind="visible: parentId() !== 0 && (!topLevelChild() || !firstSibling())" class="divider"></dd>
                <dd><a href="#" data-toggle="pill" data-target="#item-pane" title="#{TocEdit}" class="edit">#{TocEdit}</a></dd>
                <dd data-bind="visible: parentId() !== 0"><a href="#" class="remove">Remove</a></dd>
                <dd class="divider"></dd>
                <dd><a href="#page" data-toggle="pill" data-target="#item-pane" title="#{TocAddPage}" class="add">#{TocAddPage}</a></dd>
                <dd><a href="#video" data-toggle="pill" data-target="#item-pane" title="#{TocAddVideo}" class="add">#{TocAddVideo}</a></dd>
                <dd><a href="#file" data-toggle="pill" data-target="#item-pane" title="#{TocAddFile}" class="add">#{TocAddFile}</a></dd>
                <dd><a href="#test" data-toggle="pill" data-target="#item-pane" title="#{TocAddTest}" class="add">#{TocAddTest}</a></dd>
                <dd><a href="#data" data-toggle="pill" data-target="#item-pane" title="#{TocAddData}" class="add">#{TocAddData}</a></dd>
                <dd class="divider"></dd>                                
                <dd><a href="#library" data-toggle="pill" data-target="#item-pane" title="#{TocAddLibrary}" class="add add-from-library">#{TocAddLibrary}</a></dd>
            </dl>                            
        </div>
        <span class="divider" data-bind="visible: type() == 'settings'"></span>
        <ol class="toc-child" data-bind="sortable: {template: 'listTemplate', data:children, isEnabled: $root.allowEditing(), beforeMove: beforeTreeItemMove, afterMove: tocSorted, allowDrop: false }"></ol>
    </li>
</script>

2 个答案:

答案 0 :(得分:20)

似乎IE8被单词class搞糊涂了,所以它需要在引号中。所以

<li data-bind="attr: { class: id() == $root.currentItem().id() ? 'active' : '' }">

变为

<li data-bind="attr: { 'class': id() == $root.currentItem().id() ? 'active' : '' }">

答案 1 :(得分:4)

你是否有理由远离淘汰赛的“css”绑定

http://knockoutjs.com/documentation/css-binding.html

很高兴知道你在IE中发现了问题,并找到了解决方法, 但因为它不在“attr”绑定的文档中, 并且“css”绑定似乎是淘汰推荐的课程 应用类名的动作,可能不会令人困惑 为了你或其他人在路上?

来自knockout documentation for "css" binding

  

“的用途

     

css绑定将一个或多个命名的 CSS类 [强调添加]添加或删除到关联的DOM元素。例如,如果它变为负值,则以红色突出显示某些值很有用。

     

(注意:如果您不想应用CSS类,而是想直接指定样式属性值,请参阅样式绑定。)“