首页 > 解决方案 > 图像地图区域元素上的 Onclick 事件

问题描述

我有以下问题。我有一个大图像(横幅),上面有两个标志,它们是 2 个地图区域。我想做以下事情:当您单击英语标志时,语言变为英语,当您单击西班牙标志时,语言变为西班牙语。我不知道我是否应该使用 href 或 onclick 功能。我添加了一些代码。

<img src="banner_trading_1440_x_176_esp.jpg"   usemap="#planetmap">
<map name="planetmap">
 <area shape="rect" coords="1318,14,1346,32" alt="Eng"  href="javascript:window.location.href='<?php echo base_url(); ?> LanguageSwitcher/switchLang/spanish';" >
 <area shape="rect" coords="1276,14,1303,32" alt="Esp" onclick="javascript:window.location.href='<?php echo base_url(); ?> LanguageSwitcher/switchLang/spanish';"/>
</map>

在我将两个标志都作为 2 个单独的图像之前,代码是这样的(并且它工作得很好):

  <img style="width: 30px; cursor: pointer; padding-top: 15px; padding-bottom: 15px;" src="http://www.hemptrading.com/tienda3/asserts/img/spain.png" onclick="javascript:window.location.href='<?php echo base_url(); ?> LanguageSwitcher/switchLang/spanish';"/> 

标签: javascripthtml

解决方案


You should have to call a javascript function from area tag and put your php code in that function definition

<area shape="rect" coords="1318,14,1346,32" alt="Eng" href="javascript:your_function();" >

and add this function of php code

<script> function your_function() { window.location.href='<?php echo base_url(); ?> LanguageSwitcher/switchLang/spanish'; } </script>


推荐阅读