Jquery can't get my head around this

前端 未结 4 760
予麋鹿
予麋鹿 2021-01-20 04:26

The alert(i) onclick bind line is run on 3 divs, but all of them when clicked alert the last set value of i. I hope what I\'m trying to do makes s

4条回答
  •  面向向阳花
    2021-01-20 04:52

    On a tangent, a better way to add click handlers to multiple markers would be add a single click handler to just their container. The HTML could look something like:

    ...
    ...
    ...

    You can use generic HTML5 data-* attributes to store arbitrary data.

    And the JavaScript would be:

    // Updates bar preview box
    this.updatePropertyMarkerBox = function (self, BarsID) {
    
        ... snip ...
    
        // Add event handler
        $("#bmi-" + self.containerId).click(function (event) {
            var marker = $(event.target).attr("data-marker");
    
            if (marker) { // Hey, it's a marker!
                alert(marker);
            }
        });
    

提交回复
热议问题