从淘汰的给定元素中选择

时间:2014-02-05 15:59:35

标签: jquery jquery-mobile knockout.js

编辑: 我通过直接绑定到需要更改的元素来解决我的实际问题,从而无需进行任何选择。所以我现在很好。但是,如果有人能够在一开始就告诉我什么是错的,我仍然会很高兴看到答案:)

我正在尝试对所有匹配选择器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] example 1

[2] example 2

[3] example 3

我做错了什么?我已经搜索了很多,因为“从给定的音符中选择的东西很热”,并找到了我正在尝试的方法,但都没有用。我完全没有想法。

1 个答案:

答案 0 :(得分:0)

我猜这些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']"));
});