Javascript jQuery问题

Javascript jQuery问题,javascript,jquery,Javascript,Jquery,下面我给你一些代码,这些代码已经被我彻底销毁了 $(".gig").hover(function() { var id = $(this).attr("id"); var time = $(this).attr("title"); function () { if (time > 0) { var answer = $('<div class="roll">Are you attending? &

下面我给你一些代码,这些代码已经被我彻底销毁了

$(".gig").hover(function() {
    var id = $(this).attr("id");
    var time = $(this).attr("title");

    function () {
        if (time > 0)
        {
            var answer = $('<div class="roll">Are you attending? <a href="' + id + '">Yes</a></div>').hide();

        }
        else
        {
            var answer = $('<div class="roll">Did you attend?<a href="' + id + '">Yes</a> </div>').hide();              
        }

        answer.appendTo($(this)).fadeIn("fast");
    } 
    function () {
        $(this).find("div:last").fadeOut("fast", function() {
            $(this).remove()
        });
    }
});
$(“.gig”).hover(函数(){
var id=$(this.attr(“id”);
var time=$(this.attr(“title”);
函数(){
如果(时间>0)
{
var answer=$(“您参加了吗?”).hide();
}
其他的
{
var answer=$(“你参加了吗?”).hide();
}
答案。追加($(本)).fadeIn(“快速”);
} 
函数(){
$(this.find(“div:last”).fadeOut(“fast”,function()){
$(this.remove())
});
}
});
我知道,这太可怕了。现在它基本上就像伪代码。有没有人能帮我把这些乱七八糟的东西重新写进实际运行的代码

基本上,对于每个名为div的类,它应该将变量answer附加到此div的末尾,并在mouseout上删除变量answer。变量time将从rollover div的标题中获取,从而影响添加的新div中显示的内容。.gig的id应该简单地传递到url中

悬停应答添加/删除函数独立工作,但我无法将它们放入带有if和新变量的新函数中

非常感谢。

编辑

你已经编辑了你的问题。我认为这应该满足您的要求:

$(".gig").hover(function() {
    var $this = $(this);
    var id = $this.attr("id");
    var time = $this.attr("title");
    var a = $('<a />').text('Yes').attr('href', id);
    var div = ('<div />').addClass('roll');
    if(time > 0){
        div.text('Are you attending?');
    } else {
        div.text('Did you attend?');
    }
    var answer = div.append(a).hide();
    answer.appendTo( this ).fadeIn('fast');
}, function(){
    $(this).find("div.roll:last").fadeOut("fast", function() {
            $(this).remove()
    });
});
$(“.gig”).hover(函数(){
var$this=$(this);
var id=$this.attr(“id”);
变量时间=$this.attr(“标题”);
var a=$('').text('Yes').attr('href',id);
var div=(“”).addClass('roll');
如果(时间>0){
div.text('你参加了吗?');
}否则{
div.text('你参加了吗?');
}
var answer=div.append(a.hide();
答案.appendTo(this).fadeIn('fast');
},函数(){
$(this).find(“div.roll:last”).fadeOut(“fast”,function(){
$(this.remove())
});
});
不过,您可以使用一些缓存来创建DOM节点,例如:

$(".gig").hover(function() {
    var $this = $(this);
    if(typeof $this.data('attendingLink') === 'undefined'){
        var id = $this.attr("id");
        var time = $this.attr("title");
        var a = $('<a />').text('Yes').attr('href', id);
        var div = ('<div />').addClass('roll');
        if(time > 0){
            div.text('Are you attending?');
        } else {
            div.text('Did you attend?');
        }
        var answer = div.append(a);
        $this.data('attendingLink', answer);
    } else {
        var answer = $this.data('attendingLink');
    }
    answer.hide();
    answer.appendTo( this ).fadeIn('fast');
}, function(){
    $(this).find("div.roll:last").fadeOut("fast", function() {
            $(this).remove()
    });
});
$(“.gig”).hover(函数(){
var$this=$(this);
if(typeof$this.data('AttentingLink')=='undefined'){
var id=$this.attr(“id”);
变量时间=$this.attr(“标题”);
var a=$('').text('Yes').attr('href',id);
var div=(“”).addClass('roll');
如果(时间>0){
div.text('你参加了吗?');
}否则{
div.text('你参加了吗?');
}
var answer=div.append(a);
$this.data('attentinglink',应答);
}否则{
var answer=$this.data('attentinglink');
}
回答。隐藏();
答案.appendTo(this).fadeIn('fast');
},函数(){
$(this).find(“div.roll:last”).fadeOut(“fast”,function(){
$(this.remove())
});
});
然而,这只会在用户将鼠标悬停在.gig元素上方时改善性能。

编辑

你已经编辑了你的问题。我认为这应该满足您的要求:

$(".gig").hover(function() {
    var $this = $(this);
    var id = $this.attr("id");
    var time = $this.attr("title");
    var a = $('<a />').text('Yes').attr('href', id);
    var div = ('<div />').addClass('roll');
    if(time > 0){
        div.text('Are you attending?');
    } else {
        div.text('Did you attend?');
    }
    var answer = div.append(a).hide();
    answer.appendTo( this ).fadeIn('fast');
}, function(){
    $(this).find("div.roll:last").fadeOut("fast", function() {
            $(this).remove()
    });
});
$(“.gig”).hover(函数(){
var$this=$(this);
var id=$this.attr(“id”);
变量时间=$this.attr(“标题”);
var a=$('').text('Yes').attr('href',id);
var div=(“”).addClass('roll');
如果(时间>0){
div.text('你参加了吗?');
}否则{
div.text('你参加了吗?');
}
var answer=div.append(a.hide();
答案.appendTo(this).fadeIn('fast');
},函数(){
$(this).find(“div.roll:last”).fadeOut(“fast”,function(){
$(this.remove())
});
});
不过,您可以使用一些缓存来创建DOM节点,例如:

$(".gig").hover(function() {
    var $this = $(this);
    if(typeof $this.data('attendingLink') === 'undefined'){
        var id = $this.attr("id");
        var time = $this.attr("title");
        var a = $('<a />').text('Yes').attr('href', id);
        var div = ('<div />').addClass('roll');
        if(time > 0){
            div.text('Are you attending?');
        } else {
            div.text('Did you attend?');
        }
        var answer = div.append(a);
        $this.data('attendingLink', answer);
    } else {
        var answer = $this.data('attendingLink');
    }
    answer.hide();
    answer.appendTo( this ).fadeIn('fast');
}, function(){
    $(this).find("div.roll:last").fadeOut("fast", function() {
            $(this).remove()
    });
});
$(“.gig”).hover(函数(){
var$this=$(this);
if(typeof$this.data('AttentingLink')=='undefined'){
var id=$this.attr(“id”);
变量时间=$this.attr(“标题”);
var a=$('').text('Yes').attr('href',id);
var div=(“”).addClass('roll');
如果(时间>0){
div.text('你参加了吗?');
}否则{
div.text('你参加了吗?');
}
var answer=div.append(a);
$this.data('attentinglink',应答);
}否则{
var answer=$this.data('attentinglink');
}
回答。隐藏();
答案.appendTo(this).fadeIn('fast');
},函数(){
$(this).find(“div.roll:last”).fadeOut(“fast”,function(){
$(this.remove())
});
});

然而,这只会在用户将鼠标悬停在.gig元素上时提高性能。

这部分内容突然出现在我面前:
。id.
-在Javascript中,您不使用
连接字符串,而是使用
+

事实上,您发布了带有该错误的代码,这告诉我A)您有一个平淡无奇的编辑器,和/或B)您没有使用类似的工具调试Javascript。你可能应该坐上去。:)

您还试图错误地使用hover的两个函数参数,因为您将所有代码放在一个大函数中,然后只声明两个匿名函数(我甚至不确定语法是否有效??)

下面是代码的正确版本(就语法和我认为的逻辑而言)。试一试

$(".gig").hover(function () {
    var id = $(this).attr("id");
    if (time > 0) {
        var answer = $('<div class="roll">Are you attending? <a href="' + id + '">Yes</a></div>').hide();
    } else {
        var answer = $('<div class="roll">Did you attend?<a href="' + id + '">Yes</a> </div>').hide();                          
    }
    answer.appendTo($(this)).fadeIn("fast");
}, function () {
    $(this).find("div:last").fadeOut("fast", function() {
        $(this).remove()
    });
});
$(.gig”).hover(函数(){
var id=$(this.attr(“id”);
如果(时间>0){
var answer=$(“您参加了吗?”).hide();
}否则{
var answer=$(“你参加了吗?”).hide();
}
答案。追加($(本)).fadeIn(“快速”);
},函数(){
$(this.find(“div:last”).fadeOut(“fast”,function()){
$(this.remove())
});
});

这部分突然跳到我身上:
。id.
-在Javascript中,您不使用
连接字符串,而是使用
+

事实上,您发布了带有该错误的代码,这告诉我A)您有一个平淡无奇的编辑器,和/或B)您没有使用类似的工具调试Javascript。你可能应该坐上去。:)

您还试图错误地使用hover的两个函数参数,因为您将所有代码放在一个巨大的函数中,并且