Knockout.js get dom object associated with data

前端 未结 5 2047
别那么骄傲
别那么骄傲 2020-12-23 13:28

I\'m working with knockout.js to build dynamic lists and I\'m trying to figure out how I can get the DOM object associated with an object in my observable array. Specifical

5条回答
  •  鱼传尺愫
    2020-12-23 13:59

    The $(event.target) solution is good if it is related to an already occurring event in which the item's DOM element is at target. But sometimes you don't have the targeted item because there is no event (for example - you want to scroll a list to an item that was not gestured by the user).

    In such case you can give the item's DOM element id attribute a unique value that contains the item id:

  • and then getDomObject() looks like:

    getDomObject = function(item) { return $("#item_" + item.id); }
    

提交回复
热议问题