2010-05-31 60 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点击下一步按钮,你的网站将仍然有效。