admin管理员组

文章数量:1279148

I am trying to get the class or an id of the last clicked element. This is what I have based off of what I found here...

HTML

<a href="" class="button">Button</a>

JQUERY

$('.button').click(function () {
          myFuntion();
});

function myFunction (e) {
 e = e || event;
 $.lastClicked = e.target || e.srcElement;

 var lastClickedElement = $.lastClicked;
 console.log(lastClickedElement);

}

This sort of does what I want, but I am not sure how to go about modifying it so I can get just the class.

I have also tried using this solution but couldn't get it to work with my code.

$('.button').click(function () {
  myFuntion();
});


function myFunction(){
    var lastID;
    lastID = $(this).attr("id");

    console.log(lastID);
}

When I do this my console log es back as undefined. I am probably missing something obvious. Any help is much appreciated. Thanks.

I am trying to get the class or an id of the last clicked element. This is what I have based off of what I found here...

HTML

<a href="" class="button">Button</a>

JQUERY

$('.button').click(function () {
          myFuntion();
});

function myFunction (e) {
 e = e || event;
 $.lastClicked = e.target || e.srcElement;

 var lastClickedElement = $.lastClicked;
 console.log(lastClickedElement);

}

This sort of does what I want, but I am not sure how to go about modifying it so I can get just the class.

I have also tried using this solution but couldn't get it to work with my code.

$('.button').click(function () {
  myFuntion();
});


function myFunction(){
    var lastID;
    lastID = $(this).attr("id");

    console.log(lastID);
}

When I do this my console log es back as undefined. I am probably missing something obvious. Any help is much appreciated. Thanks.

Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked Aug 29, 2012 at 15:48 khollenbeckkhollenbeck 16.2k18 gold badges68 silver badges102 bronze badges 1
  • Thanks everybody for the fast responses. I will look through these starting with the first reply and see if I can get anything to work. – khollenbeck Commented Aug 29, 2012 at 16:00
Add a ment  | 

5 Answers 5

Reset to default 1

You can pass clicked element as parameter to your function:

$('.button').click(function () {
    myFunction(this);
});

function myFunction(element) {
    console.log(element);
    console.log(element.id);
    console.log($(element).attr("class"));
}

UPDATE added jsfiddle

A couple of ways e to mind:

$(".button").click(myFunction);

Should work with the above myFunction.


$(".button").click(function () { myFunction($(this)); });
function myFunction($elem) {
   var lastID;
   lastID = $elem.attr('id');
   $.data('lastID', lastID);
}

In order to get the class-name of the element, assuming you have an accurate reference to the element from which you want to retrieve the data:

var lastClickedElement = $.lastClicked,
    lastClickedElementClassNames = lastClickedElement.className;

This does return the full list of all the classes of the element though.

$('.button').click(function () {
  myFuntion(this);
});


function myFunction(ele){
    var lastID;
    lastID = $(ele).attr("id");

    console.log(lastID);
}

First Select all possible DOM Elements

 var lastSelectedElement = null

    $(document).ready(function(){
       $("*").live("click",function(){
       lastSelectedElement = $(this);
       myFunction($(this));
      });
    });

   function myFunction(element) {
console.log(element);
console.log(element.id);
console.log($(element).attr("class"));
 }

than you could play with lastSelectedElement by grabbing it's ID or Class with jQuery .attr("ID OR CLASS");

本文标签: javascriptjQuery how to get the class or id of last clicked elementStack Overflow