jQuery - how to determine which link was clicked

前端 未结 4 1208
一向
一向 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:15

    Am not sure I fully understand what it is you are having difficulty with, but the following is how I would do it.

    Click Here to See Data

    $(document).ready(function() { $(document).on('click', 'p.ShowSDB_L2', function(evt) { var $p = $(evt.currentTarget), dbG = $p.data('dbg'), slid = $p.data('slid'), $div = $p.next(); FSD_L2(dbG, slid, $div); }); }); function FSD_L2(dbG, SlID, $div) { $div.load("test15.php?dbG="+dbG+"&SlID="+SlID).css('display','block'); }

    The click handler is not hardcoded to each p tag. Instead with each p tag we store the required data, ie dbg & slid.

    The click handler is then attached once at document ready. jQuery abstracts over the various browsers and passes to its handlers the event object as its first parameter. This object can then be used to find the element on which the event occurred. Refer: http://api.jquery.com/on/

    Finally, we fetch the required data from the clicked element, find the div that needs to be updated and then call your custom function.

提交回复
热议问题