2015-02-17 117 views
0

參照下面的小提琴: enter link description hereKnockoutJs模板和事件綁定

<div id="test"> 
    <li>gokul</li> 
</div> 

<script id="someTemplate" type="text/html"> 
     <li data-bind="text: name"></li> 
</script> 


var data = [ { name: "one" }, { name: "two" } ]; 

var element = document.getElementById("test"); 
var startForEach = document.createComment("ko template: {name: 'someTemplate', foreach: $data }"); 
var endForEach = document.createComment("/ko"); 

element.appendChild(startForEach); 
element.appendChild(endForEach); 

ko.applyBindings(data, document.getElementById("test")); 

我們列表項目,我需要根據一些JavaScript邏輯添加類。例如,我們有elementArray1 = [100,200,300],elementArray2 = [500,800,700] ElementSearch =一些隨機數如果這個隨機數在這兩個元素數組中的任意一箇中,我們需要將類「激活」添加到列表中項目。

回答

0
<div id="20" data-pid="400"> 
<div id="test"> 
    <li>gokul</li> 
    </div></div> 

<script id="someTemplate" type="text/html"> 
     <li data-bind="text: name"></li> 
</script> 


var data = [ { name: "one" }, { name: "two" } ]; 
data.myPostProcessingLogic = function (element) { 
    val=$(element).parent().parent().attr('data-pid'); 
    if(val==400){ 
    $(element).addClass('hello'); 
     console.log($(element)); 
    } 
}; 
var element = document.getElementById("test"); 
var startForEach = document.createComment("ko template: {name: 'someTemplate', foreach: $data, afterRender: myPostProcessingLogic }"); 
var endForEach = document.createComment("/ko"); 

element.appendChild(startForEach); 
element.appendChild(endForEach); 

ko.applyBindings(data, document.getElementById("test"));