如何将jQuery绑定到ONE链接到onclick函数?

时间:2021-07-08 23:56:57

How do I bind a function using jQuery to one and only one link label in my HTML document which has several links?

如何使用jQuery将函数绑定到我的HTML文档中的一个且只有一个链接标签,该标签有多个链接?

My code looks like this;

我的代码看起来像这样;

$("a").click(function(){
  $("#login").slidedown("slow");
});

But this binds all the links in the document.

但这会绑定文档中的所有链接。

2 个解决方案

#1


To expand on Michael, you'll want to add a return false;

要扩展Michael,你需要添加一个返回false;

$("#clicked_link").click(function(){
    $("#login").slidedown("slow");
    return false;
});

Otherwise when you click the link the browser will still attempt to follow the link and you'll lose the javascript action.

否则,当您单击该链接时,浏览器仍将尝试关注该链接,您将丢失该javascript操作。

#2


Name your anchor/link that has the click event with an id attribute.

为具有id属性的click事件命名您的锚点/链接。

So for instance:

例如:

<a href="..." id="clicked_link">...</a>

And then use the following jquery statement:

然后使用以下jquery语句:

$("#clicked_link").click(function(){ $("#login").slidedown("slow"); });

Easy as pie!

非常简单!

#1


To expand on Michael, you'll want to add a return false;

要扩展Michael,你需要添加一个返回false;

$("#clicked_link").click(function(){
    $("#login").slidedown("slow");
    return false;
});

Otherwise when you click the link the browser will still attempt to follow the link and you'll lose the javascript action.

否则,当您单击该链接时,浏览器仍将尝试关注该链接,您将丢失该javascript操作。

#2


Name your anchor/link that has the click event with an id attribute.

为具有id属性的click事件命名您的锚点/链接。

So for instance:

例如:

<a href="..." id="clicked_link">...</a>

And then use the following jquery statement:

然后使用以下jquery语句:

$("#clicked_link").click(function(){ $("#login").slidedown("slow"); });

Easy as pie!

非常简单!