代码之家  ›  专栏  ›  技术社区  ›  Brendan Gooden

Knockout BeforeRemove返回textNode而不是elementNode

  •  0
  • Brendan Gooden  · 技术社区  · 8 年前

    当使用Knockout的foreach绑定时,我试图在将项添加到列表中时复制分类黄色fadein。(来自 Here )

    这是我的密码

    <div id="minicartItems" data-bind="template: { foreach: DisplayItems, beforeRemove: ElementFadeOut, afterAdd: ElementFadeIn }">
            <div data-bind="attr: { id: 'sideCartItm' + $index() }">
                <!-- ko if: IsFleet() -->
                     <!-- DO STUFF -->
                <!-- /ko -->
                <!-- ko ifnot: IsFleet() -->
                    <!-- DO STUFF -->
                <!-- /ko -->
            </div>
    </div>
    

    在我的虚拟机中:

    self.ElementFadeOut = function (element, index, data) {
        $(element).fadeOut();
        // $(element.id).fadeOut();   ADDING .ID  doesnt work either because its text node.
    }
    
    self.ElementFadeIn = function (element, index, data) {
        $(element)
            .animate({ backgroundColor: 'yellow' }, 200)
            .animate({ backgroundColor: 'white' }, 800);
    }
    

    但是,在添加或删除项目时,控制台上出现了错误(并且淡入不会发生)。

    jquery-1.8.3.min.js:2 Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
        at Dt (jquery-1.8.3.min.js:2)
        at Function.css (jquery-1.8.3.min.js:2)
        at Gt (jquery-1.8.3.min.js:2)
        at Object.Gn (jquery-1.8.3.min.js:2)
        at Kn (jquery-1.8.3.min.js:2)
        at Text.o (jquery-1.8.3.min.js:2)
        at Function.dequeue (jquery-1.8.3.min.js:2)
        at Text.<anonymous> (jquery-1.8.3.min.js:2)
        at Function.each (jquery-1.8.3.min.js:2)
        at init.each (jquery-1.8.3.min.js:2)
    

    从我的调查来看 element 参数不是元素类型,而是textnode类型。

    这是一张

    enter image description here

    如果我扩展 parentNode child nodes 我可以看到有文本和div节点,但我不知道为什么。

    enter image description here

    如何让函数发送元素节点,而不是文本节点?

    1 回复  |  直到 8 年前
    推荐文章