【问题标题】:How can I use an image map to show/hide a particular div?如何使用图像映射来显示/隐藏特定的 div?
【发布时间】:2013-11-15 19:57:00
【问题描述】:

我有一个图像映射,并且希望能够根据我单击的图像映射中的哪个项目来显示/隐藏一个 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();
}

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

这里是a fiddle

谢谢!!!

【问题讨论】:

    标签: jquery html button imagemap


    【解决方案1】:

    其他人发布了类似的答案。我不确定它为什么被删除。它似乎工作:

    $('[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/

    编辑:

    为了让事情更加动态,您可以从悬停的&lt;area&gt; 中抓取“项目”,并使用该值显示适当的&lt;div&gt;

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

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

    【讨论】:

    • 现在我理解得更好了,因为我将有大约 30 个区域,所以我需要向该区域添加一个类并触发翻转。所以我在区域标签中添加了 class="state"。那么我翻转任何“状态”类,但在这种情况下如何访问项目值?再次感谢!
    • @Layne 为此,您必须参考我的回答:)
    【解决方案2】:

    您必须更改图像映射规范。像这样:

    <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" id="texasArea"/>
    </map>
    

    然后在你的jQuery中,你有一个语法错误,你需要绑定地图,像这样:

    编辑:

    $("map#usaMap").click(function(ev){
      var target = $(ev.target);
      var targetId = target.attr('id');
      if(targetId == 'texasArea') {
        $("#texas").show();
      }
    });
    

    Check this UPDATED JSFiddle 我为你构建的。

    【讨论】:

    • 请查看我更新的答案。现在我们检查是否点击了德克萨斯地区。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多