2010-08-09 88 views
0

假设我有3个li。我想要做的是当我点击任何李然后我想知道哪个李被点击并根据它添加一个事件。我如何用jQuery做到这一点,任何帮助或建议请找到哪个li被点击

+0

可能重复[JavaScript:Get clicked element](http://stackoverflow.com/questions/3758890/javascript-get-clicked-element) – bfavaretto 2012-08-20 14:32:47

回答

2

jQuery将自动捕获你的包裹组指定点击的元素:你需要提供你的HTML

$('ul li').click(function(){ 
    alert('I was clicked, my text is: ' + $(this).text()); 
}); 

See the example here.

准确地标记你需要的标记。

多个读数:

3

在jQuery中,事件处理程序内的关键字是指触发事件的元件。

$('li').click(function() { 
    alert($(this).text()); // read out the text of the list item that was clicked 
}