如何将一个div的Id传递给一个处理jQuery中单击的函数

Ank*_*kur 2 jquery function

我希望我的div id对应于数据库中的项目.基于id,我的应用程序将从数据库中提取适当的内容.

因此,当我使用.click()来处理点击时,我需要将被点击的div的Id传递给处理它的函数.我该怎么做呢?

CMS*_*CMS 6

click事件处理函数内部,上下文(this关键字)引用触发事件的DOM元素,您可以从中获取id,例如:

$('div[id]').click(function () { // all divs that have an id attribute
  var id = this.id; // or $(this).attr('id');

  someOtherFunction(id);
});
Run Code Online (Sandbox Code Playgroud)