jquery - 如何使用图像映射来显示/隐藏特定的 div?

标签 jquery html button imagemap

我有一个图像映射,并且希望能够根据我点击图像映射中的哪个项目来显示/隐藏 div?

html...

<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/9/94/U.S._Territorial_Acquisitions.png/320px-U.S._Territorial_Acquisitions.png" alt="" usemap="#texasMap"/>

<map id="texasMap" name="Texas">
    <area shape="circle" coords="135,150,45" href="#" alt="" item="texas"/>
</map> 

<div id="texas">You clicked the Republic of Texas!</div>

jquery 到目前为止...

$("#texas").hide();

$("???").click(function(){
    $("#texas").show();
}

我想我想从 map 或类似的东西中抓取“项目”标签,并使用它来知道要显示哪个 div。但是,我不确定如何调用电话。另外,我什至不确定这是否是最好的方法,因此我们将不胜感激任何建议。

这是 a fiddle !

谢谢!!!

最佳答案

其他人也发表了类似的回答。我不确定为什么它被删除了。它似乎有效:

$('[item="texas"]')

这是一个例子:

<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/9/94/U.S._Territorial_Acquisitions.png/320px-U.S._Territorial_Acquisitions.png" alt="" usemap="#texasmap" id="" />

<map id="texasMap" name="texasmap">
    <area shape="circle" coords="135,150,45" href="#" alt="" item="texas" />
    <area shape="circle" coords="245,170,30" href="#" alt="" item="florida" />
</map> 

<div id="texas" class="display">You clicked the Republic of Texas!</div>
<div id="florida" class="display">You clicked Florida!</div>
$('[item="texas"]').click(function(){
    $(".display").hide();
    $("#texas").show();
    return false;
});
$('[item="florida"]').click(function(){
    $(".display").hide();
    $("#florida").show();
    return false;
});

http://jsfiddle.net/xtKXL/5/

编辑:

为了使事情更动态一点,您可以从 <area> 中获取“项目”您悬停在其上并使用该值显示适当的 <div> :

$('[item]').click(function(){
    var item=$(this).attr('item');
    $(".display").hide();
    $("#"+item).show();
    return false;
});

http://jsfiddle.net/xtKXL/6/

关于jquery - 如何使用图像映射来显示/隐藏特定的 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20009532/

相关文章:

javascript - 如何在jquery中添加多个变量

html - 为什么我不能在 IE7 中将 <td> 居中?

css - 为输入类型 =“number” 中的任何类型的小数点分隔符获取无效样式

python - 如何水平对齐按钮?

r - 如何更新 R Shiny 中的按钮标签?

javascript - 相同的函数,不同的随机结果 - jQuery

javascript - 如何暂时禁用事件?

javascript - 从第三方网站逆向工程 JavaScript

html - 为什么这在 firefox 和 chrome 中有效,但在 IE8 中无效

html - 没有通过提交按钮获取图像