javascript - 图像地图区域元素上的 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';"/>
解决方案
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>
推荐阅读
- python - 使用 Python 访问 Json 变量时出现 cx_Oracle 错误
- java - 更改 BottomNavigationView 中的片段时如何保存数据?
- java - 异或交换代码与java中的临时变量交换代码
- swift - Swift Closures: Must Capture Lists Be Exhaustive?
- javascript - Office.js POST 方法被转换为 GET
- python - Alembic 降级、升级、修订和迁移无济于事
- clonezilla - 如何更改 Clonezilla 默认菜单选择项
- javascript - 为什么javascript`for`循环快速静音/取消静音视频
- php - Laravel 试图检索模型的 ID 但不工作?
- sql - 使用联合将不同的查询与不同的组连接起来