可能重复:
Dynamically retrieve the position (X,Y) of an HTML element
在一个MVC4应用程序(针对IE7 +)中,我有一个图像。我希望用户能够点击图像来定位一个小标记(图像),我想存储与图像相关的相对x / y位置,这样当用户返回到此页面时我可以重新定位此图钉试。
实现这一目标的最佳方法是什么?
答案 0 :(得分:1)
您可以使用<input type="image" />
标记 - 这类似于<input type="submit" />
,但它会将点击的x和y坐标发布回服务器。
<h1>Click on the image to place a pin</h1>
@using(Html.BeginForm())
{
<input type="image" name="coords" src="http://placehold.it/300x300" />
}
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(FormCollection form)
{
int x = int.Parse(form["coords.x"]);
int y = int.Parse(form["coords.y"]);
// FIXME Save location of pin
return View();
}
}