Passing parameter using onclick or a click binding with KnockoutJS

前端 未结 5 2079
遥遥无期
遥遥无期 2020-12-25 09:54

I have this function:

function make(place)
{
  place.innerHTML = \"somthing\"
}

I used to do this with plain JavaScript and html:



        
5条回答
  •  [愿得一人]
    2020-12-25 10:25

    If you set up a click binding in Knockout the event is passed as the second parameter. You can use the event to obtain the element that the click occurred on and perform whatever action you want.

    Here is a fiddle that demonstrates: http://jsfiddle.net/jearles/xSKyR/

    Alternatively, you could create your own custom binding, which will receive the element it is bound to as the first parameter. On init you could attach your own click event handler to do any actions you wish.

    http://knockoutjs.com/documentation/custom-bindings.html

    HTML

    Js

    var ViewModel = function() {
        var self = this;
        self.clickMe = function(data,event) {
    
          var target = event.target || event.srcElement;
    
          if (target.nodeType == 3) // defeat Safari bug
            target = target.parentNode;
    
          target.parentNode.innerHTML = "something";
        }
    }
    
    ko.applyBindings(new ViewModel());
    

提交回复
热议问题