簡體   English   中英

帶有setInterval的jQuery可點擊鏈接

[英]jQuery clickable links with setInterval

我正在嘗試有兩個文本鏈接,它們分別指向兩個不同的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>

我相信您的問題在於:

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

首先,我建議將$ this重命名為self,以防萬一那里存在一些沖突。

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

第三,這確實可以正常工作……如注釋中的小提琴所示。

http://jsfiddle.net/athd6/

<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>

干得好

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/

再試下

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

您的意思是這樣的: http : //jsfiddle.net/rfpSL/1/

如將<p>更改為<a>並添加:

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

        $this.attr('href', $this.attr('href') == linkOne ? linkTwo : linkOne);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM