简体   繁体   English

JavaScript:为每个链接的 href 添加像素位置

[英]JavaScript: Add pixel position to href of each link

is it possible to add the coordinates of a href-link into the target-query?是否可以将 href 链接的坐标添加到目标查询中?

I mean something like this:我的意思是这样的:

<a href="index.php?action=create&id=123&x=[...]&y=[...]" id="123">Link 123</a>

This should add the coordinates of the link (id) on the page into the query via document.write() or something.这应该通过 document.write() 或其他方式将页面上链接 (id) 的坐标添加到查询中。

PS: Should work without JQuery and with IE11 :) PS:应该在没有 JQuery 和 IE11 的情况下工作:)

Thanks!谢谢!

Instead of using a bare href attach an event using javascript, grab the X & Y you want (maybe screen or client as demonstrated below) and redirect the location.href to your chosen page building up the url as needed (commented out below so you can see output).不是使用裸href使用 JavaScript 附加事件,而是获取您想要的 X 和 Y(可能是屏幕或客户端,如下所示)并将location.href重定向到您选择的页面,根据需要构建 url(在下面注释掉,以便您可以看到输出)。

 document.querySelector('#link').addEventListener("click",evt => { console.log("screen",evt.screenX,evt.screenY); console.log("client",evt.clientX,evt.clientY); //location.href = "somepage.php?x=" + evt.screenX + "&y=" + evt.screenY });
 <a href="#" id="link">Link</a>

By setting the value of location.href in the event handler the behaviour for the user will be the same as having clicked a link with an href attribute.通过在事件处理程序中设置location.href的值,用户的行为将与单击具有href属性的链接相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM