2010-05-31 57 views
2

目前,我對JavaScript的一個轉盤,當我們點擊超鏈接,這樣它的工作原理:的onMouseOver,超鏈接和rel ...幫助我:(

<div id="direction1"> 
    <p> 
    <a href="javascript:" class="carousel-control" rel="next"><img class="fleche_suiv" src="/project/images/next.png" alt="Suivant" title="Suivant" /> 
    </a> 
    </p> 
</div> 

我想用的onMouseOver執行此西奧。太 因此,我想這一點,但它不工作:

<div id="direction1"> 
    <p onMouseOver="this.getElementsByTagName('a').click()"> 
    <a href="javascript:" class="carousel-control" rel="next"><img class="fleche_suiv" src="/project/images/next.png" alt="Suivant" title="Suivant" /> 
    </a> 
    </p> 
</div> 

如何修復它???

PS:

這是有問題的JS代碼:

next: function() { 
     if (this.current) { 
      var currentIndex = this.current._index; 
      var nextIndex = (this.slides.length - 1 == currentIndex) ? (this.options.circular ? 0 : currentIndex) : currentIndex + 1; 
     } else { 
      var nextIndex = 1; 
     } 

     if (nextIndex == 0 && this.options.circular && this.options.effect != 'fade') { 
      this.scroller.scrollLeft = 0; 
      this.scroller.scrollTop = 0; 
      nextIndex = 1; 
     } 

     if (nextIndex > this.slides.length - (this.options.visibleSlides + 1)) { 
      nextIndex = this.slides.length - this.options.visibleSlides; 
     }  

     this.moveTo(this.slides[nextIndex]); 
    } 
+0

您也可以向我們展示一些您的JS代碼。 – 2010-05-31 08:41:10

回答

0
<div id="direction1"> 
    <p onMouseOver="document.getElementById('myLink').click()"> 
     <a id="myLink" href="javascript:" class="carousel-control" rel="next"><img class="fleche_suiv" src="/project/images/next.png" alt="Suivant" title="Suivant" /> 
     </a> 
    </p> 
</div> 

'的getElementsByTagName' 返回一個數組。

+0

它不起作用 – bahamut100 2010-05-31 08:46:59

+0

'javascript:'是什麼?最好只使用'#',不是嗎? – abatishchev 2010-05-31 08:48:49

+0

結果是一樣的 – bahamut100 2010-05-31 08:55:24

0

這裏有兩個問題。

正如Jeaffrey說getElementsByTagNames返回一個數組所以你的代碼可能看起來是這樣了。

<p onMouseOver="this.getElementsByTagName('a')[0].click()"> 

但是,這不會工作,因爲沒有方便的方法來在香草javascript中以編程方式生成點擊。見In JavaScript can I make a "click" event fire programmatically for a file input element?。 至少仍然有可能jQuery:http://api.jquery.com/click/,但包括這樣的依賴關係似乎矯枉過正。

最好的解決方案,我在你的情況看是隻直接調用一個[HREF]法在P [的onmouseover。

<p onMouseOver="yourObjectInWichNextIsdefined.next()"> 
1

你的代碼中的哪個地方是分配給錨元素的click事件?此外,爲什麼你只使用一個單獨的元素來分配一個mouseover事件?

你就能更好地運用

<div id="direction1"> 
    <a href="http://linktothepicture" onmouseover="yourObject.next()" class="carousel-control" rel="next"><img class="fleche_suiv" src="/project/images/next.png" alt="Suivant" title="Suivant" /></a> 
</div> 

甚至更​​好分配的JavaScript在一個單獨的文件:

window.onload = function() { 
    var nextButton = document.getElementById("idOfNextButton"); 
    nextButton.onmouseover = yourObject.next; 
}; 

這樣,當用戶未啓用和/或中等的JavaScript點擊下一步按鈕,你的網站將仍然有效。