代码如下:
-------------------------------------------------------------------------------------------------------
<div id="ID_Name_1" onmouseover="change_bg()">hello world.</div>
<div id="ID_Name_2" onmouseover="chnage_bg()">hello china.</div>
<script type="text/javascript">
    function change_bg() {
        document.getElementById().style.background="url(/images/image_name.jpg)";
    }
</script>
--------------------------------------------------------------------------------------------------------我是想当鼠标移动到两个DIV上,他们的背景图片变为image_name.jpg。我该怎么实现呢?还有我写成
---------------------------------------------------------------------------------------------------------
<div id="ID_Name_1" onmouseover="change_bg(this)">hello world.</div>
<div id="ID_Name_2" onmouseover="chnage_bg(this)">hello china.</div>
<script type="text/javascript">
    function change_bg(this) {
        document.getElementById(this).style.background="url(/images/image_name.jpg)";
    }
</script>
----------------------------------------------------------------------------------------------------------
报错了,为什么啊?this不能传递ID名吗?

解决方案 »

  1.   

    <div   id= "ID_Name_1 "   onmouseover= "change_bg(this) "> hello   world. </div> 
    <div   id= "ID_Name_2 "   onmouseover= "chnage_bg(this) "> hello   china. </div> 
    <script   type= "text/javascript "> 
            function   change_bg(ele)   { 
    alert(ele.style.background)
                    ele.style.background= "url(/images/image_name.jpg) "; 
            } 
    </script> 
      

  2.   

    报错了,为什么啊?this不能传递ID名吗? --------------------------------------不是ID 是当前对象~·
      

  3.   

    <div   id= "ID_Name_1 "   onmouseover= "change_bg(this.id) "> hello   world. </div> 
    <div   id= "ID_Name_2 "   onmouseover= "chnage_bg(this.id) "> hello   china. </div> 
    <script   type= "text/javascript "> 
            function   change_bg(id)   { 
                    document.getElementById(id).style.background= "url(/images/image_name.jpg) "; 
            } 
    </script> 
    这样是传入ID   this.id
      

  4.   

    这里面的this 等于document.getElmentById("类名") 吗?
      

  5.   


    对~·this已经是当前对象了~·