繁体   English   中英

Javascript:onmouseover函数

[英]Javascript: onmouseover function

我在动态图片上更改onmouseover和onmouseout属性时遇到问题。 我希望它的工作方式是每当我将鼠标放在图像上时,图像都必须更改,而当我将鼠标移开时,必须将其更改为原始图片。 每当我选择任何图像时,都必须将该图像更改为在图像上移动鼠标时显示的图像。 当我选择任何其他图像时,必须执行相同的过程,但必须将之前已更改的图像更改回原始图像。

我已经完成了上述所有操作,但是我的问题是,当我选择多张图片并将鼠标悬停在先前选择的图片上时,这些图片不会更改(onmouseover属性不再适用于它们)。

<script language="javascript">
    function changeleft(loca){
        var od=''
        var imgs = document.getElementById("leftsec").getElementsByTagName("img"); 
        for (var i = 0, l = imgs.length; i < l; i++) {  
            od=imgs[i].id;

            if(od==loca){
                imgs[i].src="images/"+od+"_over.gif";
                imgs[i].onmouseover="";
                imgs[i].onmouseout="";
            }else{
                od = imgs[i].id;
                imgs[i].src="images/"+od+".gif";
                this.onmouseover = function (){this.src="images/"+od+"_over.gif";};
                    this.onmouseout = function (){this.src="images/"+od+".gif";};
            }

        }
    }
</script>

<div class="leftsec" id="leftsec" >
    <img id='wits' class="wits1"  src="images/wits.gif" onmouseover="this.src='images/wits_over.gif'" onmouseout="this.src='images/wits.gif'" onclick="changeleft(this.id)" /><br />

    <img id='city' class="city1" src="images/city.gif" onmouseover="this.src='images/city_over.gif'" onmouseout="this.src='images/city.gif'" onclick="changeleft(this.id)" /><br />

    <img id='organise' class="city1" src="images/organise.gif" onmouseover="this.src='images/organise_over.gif'" onmouseout="this.src='images/organise.gif'" onclick="changeleft(this.id)" /><br />

    <img id='people' class="city1" src="images/people.gif" onmouseover="this.src='images/people_over.gif'" onmouseout="this.src='images/people.gif'" onclick="changeleft(this.id)" /><br />
</div>

我建议使用Ajax库(jQuery,YUI,dojo,ExtJS等)。 在jQuery中,我将执行以下操作:

编辑:使用.click()功能扩展了示例。

var ignoreAttrName = 'data-ignore';

var imgTags = jQuery('#leftsec img'); // Select all img tags from the div with id 'leftsec'

jQuery(imgTags)
.attr(ignoreAttrName , 'false') // Supplying an ignore attribute to the img tag
.on('click', function () {
    jQuery(imgTags).attr(ignoreAttrName, 'false'); // Resetting the data tag
    jQuery(this).attr(ignoreAttrName, 'true'); // only the current will be ignored
    // Do whatever you want on click ...
})
.on('mouseover', function () {
    // This will be called with the img dom node as the context
    var me = jQuery(this);

    if (me.attr(ignoreAttrName) === 'false') {

        me.attr('src', me.attr('id') + '.gif');
    }
})
.on('mouseout', function () {
    // This will be called when leaving the img node
    var me = jQuery(this);

    if (me.attr(ignoreAttrName) === 'false') {
        me.attr('src', me.attr('id') + '-over.gif');
    }
});

我认为,有了一个库,它更干净,更可扩展,并且它在其他浏览器中运行的机会也增加了:)。

希望这可以帮助你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM