0

我只是好奇有人会怎么做这个。我试图在缺乏知识的情况下弄清楚,当然我做不到。

所以它会像......

如果在 jquery 中声明了 onClick 函数,例如“firstGateway”和“secondGateway”,我该如何添加如果有第一个,如果有第二个怎么办。

我什至不能很好地解释。

但是让我试试。

<a onClick="firstGateway">YES FIRST!</a>

那将是 html 片段,jquery 需要运行以下内容:

<script type="text/javascript" src="http://fileice.net/gateway/mygate.php?id=492b542f45684b42"></script>
onClick=startGateway('123456');

如果它是这样的html:

<a onClick="secondGateway">YES SECOND!</a>

然后jquery会运行如下:

<script type="text/javascript" src="http://fileice.net/gateway/mygate.php?id=4465766c4278366467523838"></script>
onClick=startGateway('654321');

希望你能理解我。我仍然会努力让它发挥作用,但我认为我不会成功。

4

2 回答 2

1
$('a').click(function(e){
    if (e.target.innerHTML == "something")
        //fooo
    else
        // Bar
});

您可以在回调中检查您想要的任何内容。e.target是被点击的锚点。

if (e.target.id == "someId")
if ($(e.target).hasClass('fooClass'))
于 2012-11-04T15:43:32.110 回答
1

使用您当前的代码,如果有人单击该链接,则不会发生任何事情。让我们首先解决这个问题:

这:

<a onClick="firstGateway">YES FIRST!</a>

应该是这样的:

<a onClick="firstGateway()">YES FIRST!</a>

如果您想firstGateway()在用户单击该链接时执行该功能。但是,这仍然不是最好的方法,我将在下面向您展示更好的方法。(请注意,我的最终解决方案也需要这种更好的方法)。

现在我们把它变成:

<a id='gateway1'>YES FIRST!</a>

我们不再在 HTML 中定义事件,而是使用 jQuery 在 javascript 中定义:

$(document).ready(function ()
{
    $('a#gateway1').click = firstGateway; // Do note: this time around there are 
                                          // no brackets
}

使用它,您现在可以做几件事。首先,您可以这样做:

$('a#gateway1').click();

它模拟点击链接,我相信它会做你想做的事情。

但是,为了编写代码,您必须确保知道您在 javascript 中连接到的函数是什么,因此您甚至可能不再需要这样的解决方案,因为您应该能够做到这一点:

$(document).ready(function ()
{
    $('a#gateway1').click = firstGateway; // Do note: this time around there are 
                                          // no brackets

    firstGateway();
}
于 2012-11-04T16:07:41.520 回答