0

我正在尝试有两个文本链接,它们指向两个不同的 URL,并且文本每 7 秒从一个文本更改为另一个文本。

我尝试添加点击事件,但无法使其正常工作。

jQuery:

$(document).ready(function () {
    setInterval(function() {
$('#rollover').fadeOut(500, function() {
    var $this = $(this);
    var textOne = "Join our facebook group";
    var textTwo = "Join our LinkedIn group connect Scientists today";
    $this.text($this.text() == textOne ? textTwo : textOne);  
    $this.fadeIn(500);
});
}, 7000);   

});

html:

<p id="rollover">Join our LinkedIn group connect Scientists today</p>
4

5 回答 5

0

我相信您的问题出在这一行:

$this.text($this.text() == textOne ? textTwo : textOne);  

首先,我建议将 $this 重命名为 self,以防万一那里发生某种冲突。

self.text(self.text() == textOne ? textTwo : textOne);

第三,这确实工作正常......如评论中显示的小提琴所示。

http://jsfiddle.net/athd6/

于 2013-02-28T16:36:12.070 回答
0
<script type="text/javascript">
$(document).ready(function () 
{
      change();
});


function change(){
    setTimeout(function() {
        $('#rollover').fadeOut(500, function() {
            var $this = $(this);
            var textOne = "Join our facebook group";
            var textTwo = "Join our LinkedIn group connect Scientists today";
            $this.text($this.text() == textOne ? textTwo : textOne);  
            $this.fadeIn(500);
            change();
        });
        }, 7000); 
}
</script>
于 2013-02-28T16:41:23.293 回答
0

干得好

var textOne = "Join our facebook group";
var textTwo = "Join our LinkedIn group connect Scientists today";
var isRotated=true;
var rotateText=function(){
    setInterval(function(){        
        var text=isRotated?textOne:textTwo;
          $("#rollover").text(text);      
        isRotated=!isRotated;
    }, 7000);
};

rotateText();

http://jsfiddle.net/UQk5h/1/

于 2013-02-28T16:41:52.090 回答
-1

下一个试试

    $this.html($this.text() == textOne ? textTwo : textOne);
于 2013-02-28T16:37:28.187 回答
-1

你的意思是这样的:http: //jsfiddle.net/rfpSL/1/

如将 更改<p>为 an<a>并添加:

        var linkOne = "http://url-one.com";
        var linkTwo = "http://url-two.com";

        $this.attr('href', $this.attr('href') == linkOne ? linkTwo : linkOne);
于 2013-02-28T16:40:09.017 回答