JavaScript:onmouseover 函数

发布于 2024-12-18 18:24:14 字数 1874 浏览 6 评论 0原文

我在更改动态图片上的 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>

I have a problem with changing onmouseover and onmouseout attributes on dynamic pictures. The way i want it to work is whenever i put my mouse over images the images must change and when i take my mouse away it must be changed to the original picture. and whenever i select any image, that image must be changed to the image which was displayed while moving the mouse across the image. and when i select any other image the same process must take place but the previous image that was changed must be changed back to the original picture.

I have accomplished all of the above but my problem is when i select multiple pictures and put my mouse over images that were previously selected, those images do not change (onmouseover attribute does not work on them anymore).

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2024-12-25 18:24:15

我建议使用 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');
    }
});

我认为有了库,它会更干净、更可扩展,而且它在其他浏览器中工作的机会也增加了:)。

希望这对您有帮助!

I would recommend to use an Ajax library (jQuery, YUI, dojo, ExtJS, ...). In jQuery I would do something like:

Edit: Extended the example with .click() ability.

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');
    }
});

With a library it's cleaner and more scalable I think and the chance that it's working in other browsers is increased too :).

Hope this helps you out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文