代码简化

发布于 2024-11-02 13:50:10 字数 1569 浏览 5 评论 0原文

我正在使用带有美国地图的 jQuery maphighlight 插件。

我有几个州太小,无法在上面放置缩写,所以我必须将它们放在一边。

我已经做的是,当用户将鼠标悬停在缩写上时,相应的状态会突出显示。效果很好。

我遇到的“问题”是,虽然代码有效,但对我来说看起来太重复了,我试图简化/优化它,但我得到的错误是所有缩写都突出显示一个状态,而不是相应的状态。

这是我到目前为止的代码:

$(function() {
    $('.map').maphilight();

    $('#ma-link').mouseover(function(e) {
        $('#ma').mouseover();       
        }).mouseout(function(e) {
            $('#ma').mouseout();
        }).click(function(e) { e.preventDefault(); });

    $('#ri-link').mouseover(function(e) {
        $('#ri').mouseover();       
        }).mouseout(function(e) {
            $('#ri').mouseout();
        }).click(function(e) { e.preventDefault(); });
    $('#ct-link').mouseover(function(e) {
        $('#ct').mouseover();       
        }).mouseout(function(e) {
            $('#ct').mouseout();
        }).click(function(e) { e.preventDefault(); });

    $('#nj-link').mouseover(function(e) {
        $('#nj').mouseover();       
        }).mouseout(function(e) {
            $('#nj').mouseout();
        }).click(function(e) { e.preventDefault(); });

    $('#de-link').mouseover(function(e) {
        $('#de').mouseover();       
        }).mouseout(function(e) {
            $('#de').mouseout();
        }).click(function(e) { e.preventDefault(); });

    $('#md-link').mouseover(function(e) {
        $('#md').mouseover();       
        }).mouseout(function(e) {
            $('#md').mouseout();
        }).click(function(e) { e.preventDefault(); });

    });

有办法简化吗?

对此的任何帮助将不胜感激。

谢谢。

I'm using the jQuery maphighlight plugin with a map of the U.S.

I have several states that are too small to put their abbreviations on them, so I have to put them to the side.

What I have done already is that when the user hovers on an abbreviation, the corresponding state is highlighted. That's working fine.

The "problem" I have is that, although the code works, it looks too repetitive to me, I've tried to simplify/optimize it, but the error I get is that all abbreviations highlight one single state and not the corresponding one.

Here's the code I have so far:

$(function() {
    $('.map').maphilight();

    $('#ma-link').mouseover(function(e) {
        $('#ma').mouseover();       
        }).mouseout(function(e) {
            $('#ma').mouseout();
        }).click(function(e) { e.preventDefault(); });

    $('#ri-link').mouseover(function(e) {
        $('#ri').mouseover();       
        }).mouseout(function(e) {
            $('#ri').mouseout();
        }).click(function(e) { e.preventDefault(); });
    $('#ct-link').mouseover(function(e) {
        $('#ct').mouseover();       
        }).mouseout(function(e) {
            $('#ct').mouseout();
        }).click(function(e) { e.preventDefault(); });

    $('#nj-link').mouseover(function(e) {
        $('#nj').mouseover();       
        }).mouseout(function(e) {
            $('#nj').mouseout();
        }).click(function(e) { e.preventDefault(); });

    $('#de-link').mouseover(function(e) {
        $('#de').mouseover();       
        }).mouseout(function(e) {
            $('#de').mouseout();
        }).click(function(e) { e.preventDefault(); });

    $('#md-link').mouseover(function(e) {
        $('#md').mouseover();       
        }).mouseout(function(e) {
            $('#md').mouseout();
        }).click(function(e) { e.preventDefault(); });

    });

Is there a way to simplify this?

Any help with this will be greatly appreciated.

Thanks.

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

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

发布评论

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

评论(6

倾听心声的旋律 2024-11-09 13:50:10

无需更改您的 HTML。只需用这个替换你的块即可。

$(function() {
    $('.map').maphilight();

    $('[id$="-link"]').each(function() {
        var child = $("#" + this.id.substr(0,2));
        $(this).mouseover(function() {
             child.mouseover();
        }).mouseout(function() {
             child.mouseout();
        }).click(function(e) { e.preventDefault(); });
    });
});

No change to your HTML required. Just replace your block with this.

$(function() {
    $('.map').maphilight();

    $('[id$="-link"]').each(function() {
        var child = $("#" + this.id.substr(0,2));
        $(this).mouseover(function() {
             child.mouseover();
        }).mouseout(function() {
             child.mouseout();
        }).click(function(e) { e.preventDefault(); });
    });
});
掩耳倾听 2024-11-09 13:50:10

添加一个共享类,例如“mapItem”,并将内容附加到 this 对象。

$('.mapItem').mouseover(function(e) {
        $(this).find(selector).mouseover();       
    }).mouseout(function(e) {
        $(this).find(selector).mouseout();
    }).click(function(e) { e.preventDefault(); });

Add a shared class, like 'mapItem', and attach stuff to the this object.

$('.mapItem').mouseover(function(e) {
        $(this).find(selector).mouseover();       
    }).mouseout(function(e) {
        $(this).find(selector).mouseout();
    }).click(function(e) { e.preventDefault(); });
北斗星光 2024-11-09 13:50:10

您可以提取到 jQuery 插件:

(function($) {

    $.fn.bindMice = function(relevantSelector) {
        return this
            .mouseover(function(e) {
                $(relevantSelector).mouseover();       
            })
            .mouseout(function(e) {
                $(relevantSelector).mouseout();
            })
            .click(function(e) { e.preventDefault(); });
    });

})(jQuery);

然后您可以像这样使用:

$('#ma-link').bindMice('#ma');
$('#ri-link').bindMice('#ri'); // and so on..

这只是一种方法,还有很多方法。

You could extract to a jQuery plugin:

(function($) {

    $.fn.bindMice = function(relevantSelector) {
        return this
            .mouseover(function(e) {
                $(relevantSelector).mouseover();       
            })
            .mouseout(function(e) {
                $(relevantSelector).mouseout();
            })
            .click(function(e) { e.preventDefault(); });
    });

})(jQuery);

You could then use like so:

$('#ma-link').bindMice('#ma');
$('#ri-link').bindMice('#ri'); // and so on..

This is just one way, there are many.

温柔嚣张 2024-11-09 13:50:10

只需创建一个函数并传递状态 ID,很简单:

function hoverState(state)
    $('#'+state+'-link').mouseover(function(e) {
        $('#'+state).mouseover();       
        }).mouseout(function(e) {
            $('#'+state).mouseout();
        }).click(function(e) { e.preventDefault(); });
}

Just make a function and pass the state id, simple:

function hoverState(state)
    $('#'+state+'-link').mouseover(function(e) {
        $('#'+state).mouseover();       
        }).mouseout(function(e) {
            $('#'+state).mouseout();
        }).click(function(e) { e.preventDefault(); });
}
给妤﹃绝世温柔 2024-11-09 13:50:10

如果你在太小的州上放置一个类,你可以这样做:

$('.too_small').mouseover(function(e) {
        $(this).find('.abbr').mouseover();       
        }).mouseout(function(e) {
           $(this).find('.abbr').mouseout();
        }).click(function(e) { e.preventDefault(); });

其中 .too_small 与 $('#ri-link') 位于同一元素上,而 .abbr 位于 $('#ri-link' 等元素上)

If you put a class on the states that are too small you could do this:

$('.too_small').mouseover(function(e) {
        $(this).find('.abbr').mouseover();       
        }).mouseout(function(e) {
           $(this).find('.abbr').mouseout();
        }).click(function(e) { e.preventDefault(); });

Where .too_small is on the same element as $('#ri-link') and .abbr is on elements like $('#ri-link')

戏舞 2024-11-09 13:50:10

一般来说,更简单的方法是将 rel 属性分配给相关标签,然后只使用一个一揽子分配:

$('.link').mouseover(function(e)
{
    id = $(this).attr('rel');

    $('#' + id).mouseover();
}).mouseout(function(e) 
{
    id = $(this).attr('rel');

    $('#' + id).mouseout();
}).click(function(e) 
{ 
    e.preventDefault(); 
});

In general, the easier way would be to assign rel attributes to the relevant tags, and then just use one blanket assignment:

$('.link').mouseover(function(e)
{
    id = $(this).attr('rel');

    $('#' + id).mouseover();
}).mouseout(function(e) 
{
    id = $(this).attr('rel');

    $('#' + id).mouseout();
}).click(function(e) 
{ 
    e.preventDefault(); 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文