【问题标题】:Select from a knockout given element从剔除给定元素中选择
【发布时间】:2014-02-05 15:59:35
【问题描述】:

编辑: 我通过直接绑定到需要更改的元素上解决了我的实际问题,从而无需进行任何选择。所以我现在很好。但是,如果有人可以首先教我出了什么问题,我仍然很乐意看到答案:)

我正在尝试在与选择器 div[data-role='collapsible'] 匹配的所有元素上应用 .collapsible({refresh: true}) /p>

但是我没有选择我想要的 div。事实上,我尝试过的所有选择器(比如“div”要么一无所获,要么只返回输入元素:

ko.bindingHandlers.refreshCollapsible = {
    update: function (element, valueAccessor, allBindingsAccessor) {
        $(function() {
            // this prints [1]
            console.log(element);
            // this prints [2]
            console.log($(element));
            // these find nothing, see [3]
            console.log($(element).filter("div[data-role='collapsible']"));
            console.log($(element).find("div[data-role='collapsible']"));
            console.log($("div[data-role='collapsible']", element));
            // this one seems to return the same as $(element)
            console.log($(element, "div[data-role='collapsible']"));
    });}

[1]

[2]

[3]

我做错了什么?我已经搜索了很多“从给定笔记中选择东西的热门”并找到了我正在尝试的方法,但没有一个有效。我完全没有想法。

【问题讨论】:

    标签: jquery jquery-mobile knockout.js


    【解决方案1】:

    我猜这些 console.log 不是在 $(document).ready() 函数中完成的?

    Knockout 应用 foreach 绑定,然后 <div> 仅在文档​​准备好后才创建,因此只有这样才能通过 jQuery 在 DOM 上看到它们。

    所以试试(如果还不是这样的话):

    $(function() {
        // this prints [1]
     console.log(element);
         // this prints [2]
     console.log($(element));
     // these find nothing, see [3]
     console.log($(element).filter("div[data-role='collapsible']"));
     console.log($(element).find("div[data-role='collapsible']"));
     console.log($("div[data-role='collapsible']", element));
     // this one seems to return the same as $(element)
     console.log($(element, "div[data-role='collapsible']"));
    });
    

    【讨论】:

    • 我确实没有 onReady。我添加了它,请参阅 OP。虽然没有变化。我不太确定我明白为什么这很重要,元素是通过淘汰赛给我的。我假设它是一个准备好使用的元素,我可以在上面进行选择?
    • 很可能 jQuery Mobile 正在将元素移入和移出 DOM,因此您绑定的元素在更新时可能实际上不在 DOM 中。
    猜你喜欢
    • 2017-09-02
    • 2019-07-15
    • 2019-01-01
    • 2016-06-14
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多