代码简化
我正在使用带有美国地图的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
无需更改您的 HTML。只需用这个替换你的块即可。
No change to your HTML required. Just replace your block with this.
添加一个共享类,例如“mapItem”,并将内容附加到
this
对象。Add a shared class, like 'mapItem', and attach stuff to the
this
object.您可以提取到 jQuery 插件:
然后您可以像这样使用:
这只是一种方法,还有很多方法。
You could extract to a jQuery plugin:
You could then use like so:
This is just one way, there are many.
只需创建一个函数并传递状态 ID,很简单:
Just make a function and pass the state id, simple:
如果你在太小的州上放置一个类,你可以这样做:
其中 .too_small 与 $('#ri-link') 位于同一元素上,而 .abbr 位于 $('#ri-link' 等元素上)
If you put a class on the states that are too small you could do this:
Where .too_small is on the same element as $('#ri-link') and .abbr is on elements like $('#ri-link')
一般来说,更简单的方法是将
rel
属性分配给相关标签,然后只使用一个一揽子分配:In general, the easier way would be to assign
rel
attributes to the relevant tags, and then just use one blanket assignment: