javascript - 通过图像映射区域填写输入字段
问题描述
我正在制作一个应用程序,为在现实生活中玩游戏的玩家填写分数。观众通过点击他在每回合中击中的东西来跟踪他的得分。我遇到了两个问题:
- 如何通过单击一个区域来填写我的输入字段。
- 我如何从第 1 弯到第 2 弯等。
图像映射在四个扇区中
这是脚本:(我有一个 alert('test'); 在 ??? 空间,但这也不起作用。)
$("#test").on("click", function(e) {
e.preventDefault();
console.log("here");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img id="goal" src="http://pngimg.com/uploads/football_goal/football_goal_PNG24.png" usemap="#goal-map">
<map name="goal-map">
<area id="test" href="#" alt="300 points" title="300" coords="483,50,584,146" shape="rect" />
<area class="a" href="" alt="300 points" title="300" coords="110,49,11,145" shape="rect" />
<area class="a" href="" alt="200 points" title="200" coords="13,148,110,238" shape="rect" />
<area class="a" href="" alt="200 points" title="200" coords="484,149,584,237" shape="rect" />
</map>
<br/>
This is the input field that needs to be filled:
<p>Turn 1:
<input type="text" id="turn1"></p>
<p>Turn 2:
<input type="text" id="turn2"></p>
<p>Turn 3:
<input type="text" id="turn3"></p>
解决方案
几乎就在那里 - 你需要 jQuerify 目标:
var cnt = 0;
$("#map").on("click", function(e) {
e.preventDefault();
cnt++;
if (cnt > 3) {
alert("You had your 3 gos");
}
else {
var $tgt = $(e.target);
// console.log($tgt.attr("alt"));
$("#turn"+cnt).val($tgt.attr("title"))
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img id="goal" src="http://pngimg.com/uploads/football_goal/football_goal_PNG24.png" usemap="#goal-map">
<map name="goal-map" id="map">
<area id="test" href="#" alt="300 points" title="300" coords="483,50,584,146" shape="rect" />
<area class="a" href="" alt="300 points" title="300" coords="110,49,11,145" shape="rect" />
<area class="a" href="" alt="200 points" title="200" coords="13,148,110,238" shape="rect" />
<area class="a" href="" alt="200 points" title="200" coords="484,149,584,237" shape="rect" />
</map>
<br/>
This is the input field that needs to be filled:
<p>Turn 1:
<input type="text" id="turn1"></p>
<p>Turn 2:
<input type="text" id="turn2"></p>
<p>Turn 3:
<input type="text" id="turn3"></p>
推荐阅读
- html - 图像/文本超链接 - HTML/CSS
- javascript - FlatList 中的 ExtraData 在更改时不会更新列表
- postgresql - PostgreSQL 错误:无法扩展文件,设备上没有剩余空间
- kubernetes - Statefulset - 创建和重新启动后如何自动为 pod 设置标签?
- flutter - 如何在颤动中制作自定义和单选芯片或单选按钮
- android - 为什么 Google 要将新的开发者推向 Kotlin 的 Android 应用程序?
- laravel - 多对多中间表如何访问表中的字段?
- javascript - 如何在 Fabric JS 中获取作为 JSON 上传的 Canvas 上的项目 ID?
- validation - 通过验证从弹出窗口中捕获用户输入?
- android - 如何使用 Kotlin 将数据添加到 Android 上的 Firebase 实时数据库?