0

有人可以帮我简化我的完整工作脚本,至少我写的是交换图像吗?

就这个:

    <img id="swap_green_img" onMouseOver="swapGreen()" onMouseOut="swapOrigGreen()" onclick="window.open('http://www.stackoverflow.com', '_blank')" style="position:relative; z-index:999; float:left; margin-right:10px; cursor:pointer; height:25px; width:30px" src="facebook_like_icon_orig_50x43.png" width="30" alt="" border="0" />

<img id="swap_red_img" onMouseOver="swapRed()" onMouseOut="swapOrigRed()" onclick="window.open('http://www.stackoverflow.com', '_blank')" style="position:relative; z-index:999; float:left; margin-right:10px; cursor:pointer; height:25px; width:30px;" src="facebook_like_icon_orig_50x43.png" width="30" alt="" border="0" />

   <script type="text/javascript">

   function swapGreen() {
   document.getElementById("swap_green_img").src='facebook_like_icon_green_50x43.png';
   }

   function swapRed() {
   document.getElementById("swap_red_img").src='facebook_like_icon_red_50x43.png';
   }
   function swapOrigRed() {
   document.getElementById("swap_red_img").src='facebook_like_icon_orig_50x43.png';
   }
   function swapOrigGreen() {
   document.getElementById("swap_green_img").src='facebook_like_icon_orig_50x43.png';
   }
   </script>

首先十分感谢!托尔斯滕

4

2 回答 2

1
function swap(color){
    document.getElementById("swap_" + color + "_img").src='facebook_like_icon_' + color + '_50x43.png';
}

注意:不完全适合您的代码,但应该这样做

于 2013-02-21T17:28:55.877 回答
0
<img id="swap_green_img" onMouseOver="swap('green')" onMouseOut="swap('green', true)" onclick="window.open('http://www.google.es', '_blank')" style="position:relative; z-index:999; float:left; margin-right:10px; cursor:pointer; height:25px; width:30px" src="facebook_like_icon_orig_50x43.png" width="30" alt="" border="0" />

<img id="swap_red_img" onMouseOver="swap('red')" onMouseOut="swap('red', true)" onclick="window.open('http://www.google.es', '_blank')" style="position:relative; z-index:999; float:left; margin-right:10px; cursor:pointer; height:25px; width:30px;" src="facebook_like_icon_orig_50x43.png" width="30" alt="" border="0" />

   <script type="text/javascript">

       function swap(color, orig) {
           document.getElementById("swap_" + color + "_img").src = 'facebook_like_icon_' + (orig ? 'orig_' : '') + color + '_50x43.png';
       }
   </script>
于 2013-02-21T17:58:58.843 回答