我有大约90个项目的产品列表,其中包含src,mouseover和mouseout属性的图像。基本上mouseout src与image src相同。翻转工作正常,但我想翻转……
$('.item .product-image img').each(function(index, image){ //get the over logic var mouseover = image.getAttribute('onmouseover'); //get the out logic var mouseout = image.getAttribute('onmouseout'); //execute the over logic so it will change the src to be the over url image.onmouseover(); //swap the over and out logic for each other image.setAttribute('onmouseover', mouseout); image.setAttribute('onmouseout', mouseover); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="item"> <a href="productURL" class="product-image"> <img id="product-collection-image" src="http://imageUrl-1xxxxxxxxx.jpg" alt="product name" onmouseover="this.src='http://imageUrl-over-1xxxxxxx.jpg';" onmouseout="this.src='http://imageUrl-1xxxxxxx.jpg';" /> </a> </div> <div class="item"> <a href="productURL" class="product-image"> <img id="product-collection-image" src="http://imageUrl-2xxxxxxx.jpg" alt="product name" onmouseover="this.src='http://imageUrl-over-2xxxxxxx.jpg';" onmouseout="this.src='http://imageUrl-2xxxxxxx.jpg';" /> </a> </div> <div class="item"> <a href="productURL" class="product-image"> <img id="product-collection-image" src="http://imageUrl-3xxxxxxx.jpg" alt="product name" onmouseover="this.src='http://imageUrl-over-3xxxxxxx.jpg';" onmouseout="this.src='http://imageUrl-3xxxxxxx.jpg';" /> </a> </div>