不确定为什么在这里触发这个jQuery click事件

tim*_*one 0 javascript jquery

我创建了这个片段http://jsfiddle.net/PexkV/

<script>
var arc={};

arc.handler={
  background_color:'#8DBC8F',
  console_this: function(str){
    alert('lets write this to the console ' + str + ' ' + this.background_color);
  }

}

$(document).ready(function(){
  $('.more-click-me').on('click', arc.handler.console_this('here'));
  $('.more-click-me').on('click', function(){
    arc.handler.console_this('blue');
  });

});
</script>
<div class='more-click-me'>lets write this</div>
Run Code Online (Sandbox Code Playgroud)

并且我不确定为什么第一个事件被自动调用(示例中的"here").它似乎只应该响应点击而被调用?我得不到什么?如果我做了一些非常愚蠢的语法问题,请提前抱歉.

thx提前

Jam*_*ice 5

因为您正在调用该函数并将其返回值传递给.on().最简单的方法来实现你想实现将是一个匿名函数传递给什么.on(),就像你在第二个例子做了,打电话给你console_this在它的功能:

$('.more-click-me').on('click', function () {
    arc.handler.console_this('here');
});
Run Code Online (Sandbox Code Playgroud)

这是一个更新的小提琴.