3

In function clickCircle, i want to use the li which is being clicked. So i want to receive it as a parameter.

<li class="circle" onClick='clickCircle(this)'></li>

But what should i send as an actual paramenter? ie in place of 'this'.

4

4 回答 4

8

您可以使用this

function clickCircle(obj) // the li element clicked in the current scope
{
    var element = obj;  // This is the DOM object being clicked
    var $this = $(obj); // This is the jQuery object being clicked

    // Use DOM object like
    element.style.color="#ff0000";

    // Use jQuery object like 
    $this.css('color', 'red');
}
于 2013-07-19T19:21:16.170 回答
0

试试这个

<li class="circle"></li>

$('.circle').on('click', function(event) {
  event.target // your element
});
于 2013-07-19T19:10:44.787 回答
0

你可以像这样使用jquery来做到这一点:

$(function () {
  $('li.circle').on('click', function () {
    clickCircle($(this));
  });
});

var clickCircle = function (param) {
  // your code here
  // param.css('color', 'red').html();
};

或使用原始 javascript:

HTML

<li id="circle" class="circle"></li>

JAVASCRIPT

var circle = document.getElementById('circle');

circle.addEventListener('click', function (e) {
  clickCircle(e.target);
});

var clickCircle = function (param) {
  // your code here
  // alert(param.nodeName.toLowerCase());
}
于 2013-07-19T19:35:30.940 回答
0

您应该只使用第一个参数作为元素。

HTML

<li class="circle" onClick='clickCircle(this)'></li>

JS

clickCircle=function(element)
{
console.log(element)
console.log(element.tagName) // This will show LI
}

JSFiddle:http: //jsfiddle.net/edi9999/WhVLm/

于 2013-07-19T19:12:48.103 回答