2010-08-09 60 views
1

我的目标:鼠标悬停一个链接,在页面上有一定的元素将.class:之前的内容改为链接标题的内容。Jquery:我如何解决:在课前?

因此:

我想换一个.class的内容:之前,即。测试:之前。 但是,

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

不做这项工作。我如何在课前上课?

或者,我可以使用.before(); - 但是,我不知道如何解除.before();一旦我的鼠标脱离链接。可能是一种替代解决方案?

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

帮助赞赏!

回答

2

有趣的问题。

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

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

你能做什么,不过,仅仅是通过jQuery申请一个新的类:

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

然后在你的CSS:

#test.hover:before 

然后将过骑默认#TEST:之前

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 
     } 
    ); 

}); 

这样的事情可能会做你想要的。

+0

这就是我应该做到的!看看我是如何在那里做到的 - 它也可以工作。 – 2010-08-09 20:12:14

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>"); 

    }); 

}; 

多亏了别人!