如何两次使用同一个类来执行两个事件? jQuery的

时间:2010-02-10 21:30:12

标签: jquery

我正在尝试使用jquery中的同一个类执行两个事件...有一次让这个工作有效吗?

我需要班上的id,所以我不能用它来代替...这是一种方法来做到这一点!

$(document).ready(function() {
$("a.comm_link").anchorAnimate()
});

function open_Comm(id) {
$('.comm_link').live("click",function() {
var id = $(this).attr("id");
$("a.commscroll"+id).anchorAnimate();
$("#comment_div"+id).css('display','');
$("#commval"+id+":input:visible:enabled:first").focus();
$("#commval"+id).val("");
$("textarea#commval"+id).blur(function() {
if($("#commval"+id).val() == "") {
$("#comment_div"+id).css('display','none');
}
});
return false;
});
}

/*******
*** Anchor Slider by Cedric Dugas   ***
*** Http://www.position-absolute.com ***
Never have an anchor jumping your content, slide it.
Don't forget to put an id to your anchor !
You can use and modify this script for any project you want, but please leave this comment as credit.
*****/
jQuery.fn.anchorAnimate = function(settings) {
settings = jQuery.extend({
speed : 1200
}, settings);   
return this.each(function(){
var caller = this
$(caller).click(function (event) {  
event.preventDefault()
var locationHref = window.location.href
var elementClick = $(caller).attr("href")
var destination = $(elementClick).offset().top;
$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
window.location.hash = elementClick
});
return false;
})
})
}

继承人html:

<div class='comment' id='comment".$eid."' style='float:right;padding-right:3px;visibility:hidden;'>
<a href='#commscroll".$eid."' onclick=\"javascript:open_Comm('".$eid."');\" class='comm_link' id='".$eid."'>
<img src='http://mydomain.com/images/comment.gif' border='0' style='overflow:hidden;vertical-align:middle;text-decoration:none;border:0px;outline:none;'>Comment</a>
</div>
<div class='clear'></div>

1 个答案:

答案 0 :(得分:4)

好的,您无法在任何点击事件中使用return: false,因为这会取消默认的停止事件冒泡。

在您的live('click'...anchorAnimate click事件中,仅使用e.preventDefault

$('.comm_link').live("click",function(e) {

    ... all your code ...

    // DON'T use return false; Remove it
    e.preventDefault();
});

$(caller).click(function (event) {  
    event.preventDefault();

    ... all your code ...

    // DON'T USE return false; Remove it.
})