Jquery:我如何解决:课前?

时间:2010-08-09 18:52:47

标签: jquery class hover

我的目标:鼠标悬停一个链接,在页面上有一个元素更改.class:在内容之前从链接标题中获取内容。

因此:

我想更改.class的内容:before,即.test:before。 但是,

$("#test:before").css("content","whatever the content should be");

不能胜任这项工作。我怎么说:上课前?

或者,我可以使用.before(); - 但是,我不知道如何解开.before();一旦我的鼠标关闭了链接。可能是另类解决方案吗?

查看jquery文档 - 无法找到解决方案 - 可能是由于我有限的jquery知识;)

帮助表示赞赏!

3 个答案:

答案 0 :(得分:3)

有趣的问题。

$(x)是一个选择器......你正在寻找一个特定的DOM对象。但CSS伪类不是DOM对象。它只是CSS中的一个声明。

我不确定你可以通过jQuery直接操作它。

但是,你可以做的只是通过jQuery应用一个新类:

$("#test").hover(function(){$(this).addClass('hover')});

然后在你的CSS中:

#test.hover:before

然后将覆盖默认的#test:之前

答案 1 :(得分:2)

我不确定你正在使用的方法(因为我从未使用过:在使用jQuery操作的伪类之前)但是它使用了.hover()函数并且几乎完全相同。

$(function(){
    //once the page is ready

    //bind some hover events
    $("#test").hover(
        //hover in
        function(){
            $(this).before("<span id = 'test-before'>Before Content here</span>");
            //insert some content before "this"
        },

        //hover out
        function(){
            $("#test-before").remove();
            //remove the content we added
        }
    );

});

这样的事情可能就是你想要的。

答案 2 :(得分:0)

奥斯汀:太棒了!

我差不多走了:

this.tooltip_link = function(){ 

    $("a.tc_top_link_post").hover(function(e){                                            
        $("#acound_tc_lastfor")
            .css("background-color","#a40000")
            .css("color","white")
            .css("font-weight","bold")
            .css("padding-right","2px")
            .css("padding-left","2px");

        $("#tc_lastfor").replaceWith("<A href='#' id='tc_lastfor'>"+ this.title +"</a>");   
        $("#tc_lastfor").css("color","white");



    },

    function(){

        $("#acound_tc_lastfor")
            .css("background-color","white")
            .css("border-bottom","none")
            .css("font-weight","normal")
            .css("padding-left","0px");

        $("#tc_lastfor").replaceWith("<A href='#' id='tc_lastfor'>LAST 4 MONTHS:</a>");

   });

};

感谢其他人!