jQuery - 如何确定单击了哪个链接 [英] jQuery - how to determine which link was clicked

查看:65
本文介绍了jQuery - 如何确定单击了哪个链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的PHP片段,可生成以下代码的 n 副本:

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

<p class="ShowSDB_L2" class="center" onClick="FSD_L2('<?php print dbG;?>','<?php print $sLID;?>')">Click Here to See Data</p>   
<div class="divSDB_L2">
</div>

它是使用PHP生成的,因此预先知道副本数量。

It is generated using PHP, so the number of copies is unknown up front.

在另一个页面上,我有以下Javascript(使用jQuery)

On another page I have the following Javascript (using jQuery)

function FSD_L2(dbG,SlID)
    {
        $(".divSDB_L2").load("test15.php?dbG="+dbG+"&SlID="+SlID).css('display','block');
    }

点击上面的文字(点击这里查看数据)时,应该在两个DIV标签之间添加test15.php的内容。

When the text above (Click Here to See Data) is clicked, it should add the contents of test15.php between the the two DIV tags.

#Test15.php
<?php
$dbG = $_GET['dbG'];
$SlID = $_GET['SlID'];

print $dbG . " & " . $SlID;
?>

我遇到的问题是如何确定点击了哪些链接?目前,如果我有三个副本,然后单击一个,则会激活所有三个副本。

The problem I have is how to determine which of the links was clicked? At present, if I have three copies, and click one, all three copies are activated.

我希望我已经说清楚了。我肯定必须有一个简单的方法,但我对Javascript / jQuery很新。

I hope I have made this clear enough. I'm sure there must be a simple way, but I'm quite new to Javascript/jQuery.

推荐答案

我不确定我完全明白你遇到困难是什么,但以下是我将如何做到这一点。

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

<p class="ShowSDB_L2" class="center" data-dbg="<?php print dbG;?>" data-slid="<?php print $sLID;?>">Click Here to See Data</p>   
<div class="divSDB_L2"></div>

$(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');
}

点击处理程序没有硬编码到每个 p 标记。而是使用每个 p 标记,我们存储所需的数据,即 dbg & 滑动

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

然后在文档就绪附加一次点击处理程序。 jQuery在各种浏览器上进行抽象,并将事件对象作为其第一个参数传递给其处理程序。然后,可以使用此对象查找发生事件的元素。参考: http://api.jquery.com/on/

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/

最后,我们从点击的元素中获取所需的数据,找到需要更新的 div ,然后调用自定义函数。

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

这篇关于jQuery - 如何确定单击了哪个链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆