jQuery - how to determine which link was clicked

前端 未结 4 1243
一向
一向 2021-01-23 18:34

I have a simple piece of PHP which generates n copies of the following code:

4条回答
  •  时光取名叫无心
    2021-01-23 19:34

    Like Brian said, you could just put the same class on all of your links and use the $(this) keyword in jQuery inside of a click function to find out which link was clicked.

    Here's a basic example of changing link colors on a nav using this technique: http://jsfiddle.net/9E7WW/

    HTML:

    Test
    Test2
    Test3
    Test4
    

    Javascript:

    $(document).ready(function(){
        $('.nav').click(function(){
            // change all to black, then change the one I clicked to red
           $('.nav').css('color', 'black');
            $(this).css('color', 'red');
        });
    });
    

提交回复
热议问题