【问题标题】:Get HTML area tag name from Javascript?从 Javascript 获取 HTML 区域标签名称?
【发布时间】:2011-03-19 12:05:09
【问题描述】:

我想在有人点击后显示区域标签名称。不幸的是,当我将下面的代码用于地图区域时,我得到未定义的 obiekt 名称。这很奇怪,因为对于文本框 'kot' 它运行良好。

<script>

function metoda(obiekt)
{
    alert(obiekt.name); //Here I get undefined!
}

</script>

<input type='text' value='kot' name='das' onclick='metoda(this);'></input>
<map name='mapkama'>
   <area 
      name='AE' 
      shape='POLY' 
      coords='285,87,287,90,288,87,285,87' 
      href='#'  
      title='' 
      onclick='metoda(this); return false;'>
</map>

<img usemap="#mapkama" src='http://myimage.com/image'>

metoda 函数如何显示区域名称?

【问题讨论】:

  • 很遗憾,该区域不能被点击,除非它有一个可见的元素。
  • @rob 我已经添加了可见元素的部分,希望这对你有所帮助;)

标签: javascript html map google-visualization area


【解决方案1】:

下面的代码正确地返回了名称的值。

<script> 

function metoda(obiekt) 
{ 
    alert(obiekt.name); 
} 

</script> 

<input type='text' value='kot' name='das' onclick='metoda(this);'></input> 

<img src="planets.gif" width="145" height="126" alt="Planets" usemap="#planetmap" />

<map name="planetmap">
  <area name='AE1' shape="rect" coords="0,0,82,126" href="#" alt="Sun" onclick='metoda(this); return false;'/>
</map> 

唯一的区别是,我使用的是矩形,而您使用的是多边形。你能检查一下使用 rect 或 circle 是否对你有帮助吗?

【讨论】:

  • 这仍然返回 undefined。
  • 你的意思是矩形,它返回未定义?我注意到一件事,多边形的形状坐标需要正确才能获得点击坐标
  • @Sachin Shanbhag 与抢劫相同 - 未定义。我已将源代码更新为我所拥有的现实生活场景,因此您将能够轻松地重现它
  • 或者最好不要因为它是一堆代码。这个例子我觉得够了
  • @tomaszs - 我能够获得值 AE 作为在修改后的代码上运行时的输出。如果这有帮助,我使用的是 IE8 版本 8.0.6001.18702 浏览器。
【解决方案2】:

使用 jquery 你可以通过

<script type="text/javascript"> 

function metoda(obiekt) 
{ 
    alert(obiekt.getAttribute("name")); 
} 

</script> 

<input type='text' value='kot' name='das' onclick='metoda(this);'></input> 

<img src="planets.gif" width="145" height="126" alt="Planets" usemap="#planetmap" />

<map name="planetmap">
  <area name='AE1' shape="rect" coords="0,0,82,126" href="#" alt="Sun" onclick='metoda(this); return false;'/>
</map> 

【讨论】:

  • 嗨,我已经编辑了我的答案,obiekt.getAttribute("name") 应该这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多