Jquery添加href而不是click事件(Jquery add href instead of click event)

编程入门 行业动态 更新时间:2024-10-28 12:19:17
Jquery添加href而不是click事件(Jquery add href instead of click event)

我需要使用href而不是click事件,所以我可以在下面的语句中使用hover。

我想要做的是能够使用href,好像是click事件,所以我可以通过href附加一个函数,因此我可以使用在css上完成的悬停效果。

它是一个删除按钮,所以它是一个灰色的x,如果你悬停变红,如果你按下按钮,那么它将删除该行。

谢谢。

$(document).ready(function(){ $(".btnDeleteOther").live("click", function(){ $(this).closest('tr').not(':only-child').remove(); });

有任何想法吗?

谢谢。

I need to use href instead click event so I can use hover in the following statement.

What I am trying to do is to be able to use href as if was click event, so I can attach a function through href, therefore I could use the hover efect which is alredy done on css.

Its a delete button, so it would be a grey x and if you hover goes red, and if you press the button then it would delete the row.

Thanks.

$(document).ready(function(){ $(".btnDeleteOther").live("click", function(){ $(this).closest('tr').not(':only-child').remove(); });

Any Ideas?

Thanks.

最满意答案

在某些浏览器中:如果您没有href来获取onclick和悬停功能,则hover不起作用

<a href="#" onclick="action(); return false;">Delete X</a>

onclick上的return false会阻止浏览器执行默认操作,而href =“#”表示即使这样做也只是指向此页面的链接。

根据其他答案,您可以使用CSS hover伪类来设置元素的样式

a.delete{ ... } a.delete:hover{ ... color:red; }

对于href =“#”我会把它放在你提交的html中,如果没有你可以使用下面的jquery来添加属性

$(".btnDeleteOther").attr('href','#');

至于从click事件返回false,它确认至少当你只有一个click事件被定义时,jQuery将返回你的click函数返回的内容

$(".btnDeleteOther").click(function(){doStuff(); return false;});

或者对事件使用preventDefault方法

$(".btnDeleteOther").click(function(event){doStuff(); event.preventDefault();});

见http://api.jquery.com/click/

In some browsers a:hover does not work if you do not have an href to get the onclick and the hover functionality you can

<a href="#" onclick="action(); return false;">Delete X</a>

the return false on the onclick will prevent the browsers default action taking place and the href="#" means that even if it does this is just a link to this page.

As per the other answers you can then us the CSS hover pseudo-class to style the element

a.delete{ ... } a.delete:hover{ ... color:red; }

For the href="#" I would put this in the html you submitted if not you can use the following jquery to add the attribute

$(".btnDeleteOther").attr('href','#');

As for returning false from the click event it appares that at least when you have only one click event defined jQuery will return what your click function returns so

$(".btnDeleteOther").click(function(){doStuff(); return false;});

Or use the preventDefault method on event

$(".btnDeleteOther").click(function(event){doStuff(); event.preventDefault();});

see http://api.jquery.com/click/

更多推荐

本文发布于:2023-07-18 03:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154201.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   事件   Jquery   href   event

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!