我如何确保即使页面布局已更改,也可以将保存的单击坐标重新放置到同一位置?

我将点击坐标存储在我的数据库中,然后稍后重新加载它们,并在发生点击的站点上显示它们,如何确保它加载在同一位置?


存储单击坐标显然是简单的步骤,但是一旦用户返回并且他们的窗口较小或较大时,一旦有了它们,坐标是错误的。我是否要以错误的方式处理此问题,是否还应该存储元素ID / DOM引用或类似性质的内容?


同样,此脚本将在具有多个布局的许多不同网站上运行。在布局与坐标存储方式无关的情况下,有没有办法做到这一点?



绝地无双
浏览 444回答 3
3回答

青春有我

我更喜欢不使用id选择器,而只是递归地使用。function getPathTo(element) {&nbsp; &nbsp; if (element.tagName == 'HTML')&nbsp; &nbsp; &nbsp; &nbsp; return '/HTML[1]';&nbsp; &nbsp; if (element===document.body)&nbsp; &nbsp; &nbsp; &nbsp; return '/HTML[1]/BODY[1]';&nbsp; &nbsp; var ix= 0;&nbsp; &nbsp; var siblings= element.parentNode.childNodes;&nbsp; &nbsp; for (var i= 0; i<siblings.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var sibling= siblings[i];&nbsp; &nbsp; &nbsp; &nbsp; if (sibling===element)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';&nbsp; &nbsp; &nbsp; &nbsp; if (sibling.nodeType===1 && sibling.tagName===element.tagName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ix++;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript