对一堆按钮使用单个悬停脚本,并在鼠标悬停时使用变量?

发布于 2024-12-02 16:03:22 字数 1868 浏览 0 评论 0原文

我试图在鼠标悬停事件上定义一个变量。

我的想法是,我稍后可以使用该变量来替换 DIV ID,并使用“交换图像”路径来创建一个通用的鼠标悬停脚本,我可以将其用于一堆按钮。

因此,我不必一遍又一遍地使用相同的代码,只需切换几个数字即可。

这行得通吗?我尝试了这样的事情:

//define variable on mouseover

$("#world_map_8_button").mouseover(function() {
        var location = "#world_map_8";

    }); 


//use variable within script

   //mouse over/out swap image + 

$(location + "_button").hover(function() {
    $(location + "_button_jpg").attr("src", "img/buttons/map_buttons/world_map/world_map_8_button_over.jpg"); 
        }, function() {
            $(location + "_button_jpg").attr("src", "img/buttons/map_buttons/world_map/world_map_8_button.jpg"); 
        }); 
 });    

这是一些 html 代码来帮助澄清事情,所有按钮都以相同的方式命名。

    <div id="world_map_8_button">
            <img id="world_map_8_button_jpg" src="img/buttons/map_buttons/world_map/world_map_8_button.jpg" width="41" height="41"  alt="" />
        </div>

    <div id="world_map_9_button">
            <img id="world_map_9_button_jpg" src="img/buttons/map_buttons/world_map/world_map_9_button.jpg" width="41" height="41"  alt="" />
        </div>


    <div id="world_map_10_button">
            <img id="world_map_10_button_jpg" src="img/buttons/map_buttons/world_map/world_map_10_button.jpg" width="41" height="41"  alt="" />
        </div>

如果我在 HOVER 函数中声明变量,它会起作用(尽管仅适用于 mouseon),但这没有任何意义,也不会节省劳动力......

$("#world_map_8_button").hover(function() {

                var location = "#world_map_8";

    $((location + "_button_jpg")).attr("src", "img/buttons/map_buttons/world_map/world_map_8_button_over.jpg"); 
        },
        function() {
            $((location + "_button_jpg")).attr("src", "img/buttons/map_buttons/world_map/world_map_8_button.jpg"); 
        }); 

I'm trying to define a variable on a mouseover event.

The idea is that I can later use that variable to replace the DIV ID's, and "swap-image" pahts to create a generic mouseover script that I can use for a bunch of buttons.

So I don't have to use the same code over and over again and only swich out a few numbers.

Would this work? I tried something like this:

//define variable on mouseover

$("#world_map_8_button").mouseover(function() {
        var location = "#world_map_8";

    }); 


//use variable within script

   //mouse over/out swap image + 

$(location + "_button").hover(function() {
    $(location + "_button_jpg").attr("src", "img/buttons/map_buttons/world_map/world_map_8_button_over.jpg"); 
        }, function() {
            $(location + "_button_jpg").attr("src", "img/buttons/map_buttons/world_map/world_map_8_button.jpg"); 
        }); 
 });    

This is some of the html code to help things clear up, all buttons are named the same way.

    <div id="world_map_8_button">
            <img id="world_map_8_button_jpg" src="img/buttons/map_buttons/world_map/world_map_8_button.jpg" width="41" height="41"  alt="" />
        </div>

    <div id="world_map_9_button">
            <img id="world_map_9_button_jpg" src="img/buttons/map_buttons/world_map/world_map_9_button.jpg" width="41" height="41"  alt="" />
        </div>


    <div id="world_map_10_button">
            <img id="world_map_10_button_jpg" src="img/buttons/map_buttons/world_map/world_map_10_button.jpg" width="41" height="41"  alt="" />
        </div>

if I declare the variable in the HOVER function it works (only for the mouseon though) but that doesn't make any sense and wouldn't be labour saving...

$("#world_map_8_button").hover(function() {

                var location = "#world_map_8";

    $((location + "_button_jpg")).attr("src", "img/buttons/map_buttons/world_map/world_map_8_button_over.jpg"); 
        },
        function() {
            $((location + "_button_jpg")).attr("src", "img/buttons/map_buttons/world_map/world_map_8_button.jpg"); 
        }); 

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

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

发布评论

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

评论(3

神爱温柔 2024-12-09 16:03:22

查看您的代码,我认为属性 选择器 $=< /code> 会帮助你。您不必设置任何变量来执行此操作,试试这个

$("[id$=_button]").hover(function() {
    $("#" + this.id + "_jpg").attr("src", "img/buttons/map_buttons/world_map/" + this.id + "_button_over.jpg"); 
        }, function() {
            $"#" + this.id + "_jpg").attr("src", "img/buttons/map_buttons/world_map/" + this.id + "_button.jpg"); 
        }); 
 }); 

Looking at your code I think attribute ends with selector $= will help you. You don't have to set any variable to do this, try this

$("[id$=_button]").hover(function() {
    $("#" + this.id + "_jpg").attr("src", "img/buttons/map_buttons/world_map/" + this.id + "_button_over.jpg"); 
        }, function() {
            $"#" + this.id + "_jpg").attr("src", "img/buttons/map_buttons/world_map/" + this.id + "_button.jpg"); 
        }); 
 }); 
酸甜透明夹心 2024-12-09 16:03:22

这是 CSS 的一个很好的候选者。您可以在一张图像中同时包含悬停图像和普通图像,然后在鼠标悬停时设置背景偏移。 W3Schools 很好地解释了该技术

但是,要直接回答您的问题,您可以尝试这样的方法:

$('[id$=_button]').hover(function() {
    $('#'+this.id+'_jpg').attr('src', 'img/buttons/map_buttons/world_map/' + this.id + '_over.jpg');
}, function () {
    $('#'+this.id+'_jpg').attr('src', 'img/buttons/map_buttons/world_map/' + this.id + '.jpg');
});

您只需将其放在页面上即可处理这一切,而无需多个脚本和全局变量声明。

This is a good candidate for CSS. You could have one image with both the hover and normal images in, and then set the background offset when the mouse hovers. W3Schools explains the technique well

To answer your question directly, however, you could try something like this:

$('[id$=_button]').hover(function() {
    $('#'+this.id+'_jpg').attr('src', 'img/buttons/map_buttons/world_map/' + this.id + '_over.jpg');
}, function () {
    $('#'+this.id+'_jpg').attr('src', 'img/buttons/map_buttons/world_map/' + this.id + '.jpg');
});

You would only have to put this on your page to take care of it all without having multiple scripts and global variable declarations.

维持三分热 2024-12-09 16:03:22

不,那不行。使用 jQuery 的 .data(); 方法

No that won't work. Use jQuery's .data(); method.

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