2011-04-23 69 views
0

我寫了這個簡單的功能:的setInterval和功能

HTML:

<div class="mhead"> 
<div class="mTop"> 
    <a class="headline" title="" href=""> 
    <img alt="" src=""> 
    <span> </span> 
    </a> 
</div> 
<div class="tNav"> 
<ul id="topt" class="tUl"> 
    <li title="http://www.site.com/761028497.jpg"> <a title="" href="">1</a> </li> 
    <li title="http://www.site.com/761028497.jpg"> <a title="" href="">2</a> </li> 
</ul> 
</div> 

功能:

$.fn.inters = function (target) { 
    this.find('li').hover(function() { 
    var head = $(this).find('a').attr('title'); 
    var image = $(this).attr('title'); 
    var href = $(this).find('a').attr('href'); 
    $(target).find('img').attr('src', image); 
    $(target).find('img').attr('alt', head); 
    $(target).attr('href', href); 
    $(target).attr('title', head); 
    $(target).find('span').html(head); 
}).eq(0).hover();} 

$("#topt").inters('.headline'); 

我想補充setInterval這個腳本:

$(function() { 
setInterval("inters()", 3000); 
}); 

但標題不變。每3000毫秒如何在inters函數中進行更改?

在此先感謝

回答

2

setInterval將嘗試調用的函數inters()每3秒,這可能不是你想要的。

認爲要執行的是以下行:

$('#topt').inters('.headline'); 

如果你想這樣做,你應該創建一個將執行該行,像這樣一個功能:

setInterval(function() { 
    $('#topt').inters('.headline');  
}, 3000); 

以上將每3秒呼叫$('#topt').inters('.headline');

+0

@kevin,我已經測試你的方式和@ stecb的方式,但它不工作。我的功能有問題,但我無法找到它。 – utopia 2011-04-23 19:44:23

+0

@utopia:你能發佈受影響的HTML嗎? – Kevin 2011-04-23 19:47:03

+0

@kevin,我編輯了我的問題。謝謝。 – utopia 2011-04-23 20:09:53

0

你需要做的是這樣:

function foo(){ 
    $("#topt").inters('.headline'); 
} 

setInterval(foo,3000);