为什么我的img悬停不起作用?

时间:2017-01-01 09:25:43

标签: javascript html

我想改变我的下一个&&在on.hover上的prev img。

我曾尝试过使用JS,但似乎没有用。

PS:我为Prev和Next尝试了不同的方法

HTML

<div align='center'>
  <a href='#'>
    <img class='img-responsive pull-left'  
         src='images/arrow_left.jpg' 
         onmouseover='hover(this);' 
         onmouseout='unhover(this);'> </a>
  <ul class='pagination'>
    <li><a href='#'> 1 </a></li>
    <li><a href='#'> 2 </a></li>
    <li><a href='#'> 3 </a></li>
    <li><a href='#'>view all</a></li>
  </ul>
  <a href='#'>
    <img class='img-responsive pull-right' 
         src='images/arrow_right.jpg'  onmouseover='this.src='images/arrow_rightover.jpg'
                                                                                         onmouseout='this.src='images/arrow_right.jpg'> </a>
</div>

JS功能

function hover(element) {
element.setAttribute('src', 'images/arrow_leftover.jpg');
}

function unhover(element) {
element.setAttribute('src', 'images/arrow_left.jpg');
}

3 个答案:

答案 0 :(得分:1)

替换:

onmouseover='this.src='images/arrow_rightover.jpg' onmouseout='this.src='images/arrow_right.jpg'>

要:

onmouseover='this.src="images/arrow_rightover.jpg"' onmouseout='this.src="images/arrow_right.jpg"'>

因为浏览器“认为”该属性的值仅为this.src=

答案 1 :(得分:1)

错误的报价

  <a href='#'>
    <img class='img-responsive pull-right' 
         src='images/arrow_right.jpg'  
         onmouseover='this.src= "images/arrow_rightover.jpg"'
         onmouseout= 'this.src="images/arrow_right.jpg"'>
 </a>

答案 2 :(得分:1)

试试这个

function hover(element) {
  element.setAttribute('src', 'images/arrow_leftover.jpg');
}

function unhover(element) {
  element.setAttribute('src', 'images/arrow_left.jpg');
}
<div align='center'>
  <a href='#'>
    <img class='img-responsive pull-left' src='images/arrow_left.jpg' onmouseover='hover(this);' onmouseout='unhover(this);'>
  </a>
  <ul class='pagination'>
    <li><a href='#'> 1 </a>
    </li>
    <li><a href='#'> 2 </a>
    </li>
    <li><a href='#'> 3 </a>
    </li>
    <li><a href='#'>view all</a>
    </li>
  </ul>
  <a href='#'>
    <img class='img-responsive pull-right' src='images/arrow_right.jpg' onmouseover='this.src="images/arrow_rightover.jpg"' onmouseout='this.src="images/arrow_right.jpg"'>
  </a>
</div>