test2.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <!Doctype html>
  2. <html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8"/></head>
  3. <html>
  4. <body>
  5. <div>
  6. <ul>
  7. <li>
  8. <a href="/test.html">1</a>
  9. </li>
  10. <li>
  11. <form action="/test2.html">
  12. <input id="test" type="button" value="Ceshi"></input>
  13. </form>
  14. <p>2</p>
  15. </li>
  16. <li>
  17. <p>3</p>
  18. </li>
  19. <li>
  20. <p>4</p>
  21. <div>sdf</div>
  22. </li>
  23. </ul>
  24. </div>
  25. </body>
  26. <script>
  27. document.write("<script src='http://127.0.0.1/baozhuangscript.ts'><\/script>");
  28. </script>
  29. <!-- <script>
  30. //获取xpath
  31. function readXPath(element) {
  32. if (element.id !== "") {//判断id属性,如果这个元素有id,则显 示//*[@id="xPath"] 形式内容
  33. return '//*[@id=\"' + element.id + '\"]';
  34. }
  35. //这里需要需要主要字符串转译问题,可参考js 动态生成html时字符串和变量转译(注意引号的作用)
  36. if (element == document.body) {//递归到body处,结束递归
  37. return '/html/' + element.tagName.toLowerCase();
  38. }
  39. var ix = 1,//在nodelist中的位置,且每次点击初始化
  40. siblings = element.parentNode.childNodes;//同级的子元素
  41. for (var i = 0, l = siblings.length; i < l; i++) {
  42. var sibling = siblings[i];
  43. //如果这个元素是siblings数组中的元素,则执行递归操作
  44. if (sibling == element) {
  45. return arguments.callee(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix) + ']';
  46. //如果不符合,判断是否是element元素,并且是否是相同元素,如果是相同的就开始累加
  47. } else if (sibling.nodeType == 1 && sibling.tagName == element.tagName) {
  48. ix++;
  49. }
  50. }
  51. };
  52. var nodeList = [];
  53. NowNode= null;
  54. var xpath = "/html/body"; //记录上个元素的xpath
  55. var style = ""; //记录上个元素的颜色
  56. document.body.addEventListener("mousemove",function ()
  57. {
  58. oe = document.elementFromPoint(event.x,event.y);
  59. NowNode = oe;
  60. x = readXPath(oe);
  61. oe.style.pointerEvents = "none";
  62. if (xpath != x){
  63. try {
  64. document.evaluate(xpath, document).iterateNext().style.backgroundColor = style; //上个元素改回原来元素的背景颜色
  65. document.evaluate(xpath, document).iterateNext().style.pointerEvents = ""; //上个元素改回原来元素的背景颜色
  66. }catch{
  67. console.log("not found");
  68. }
  69. style = oe.style.backgroundColor;
  70. exist = 0;
  71. for(o of nodeList){
  72. if(o["node"] == oe){
  73. exist = 1;
  74. break;
  75. }
  76. }
  77. if(exist == 1)
  78. {
  79. console.log(123);
  80. }
  81. else
  82. {
  83. console.log("xpath:",xpath,"x:",x);
  84. oe.style.backgroundColor = '#DDDDFF'; //设置新元素的背景元素
  85. }
  86. xpath = x;
  87. }
  88. console.log("x:",x);
  89. });
  90. document.addEventListener("click",function ()
  91. {
  92. nodeList.push({node:NowNode,bgcolor:style,xpath:readXPath(NowNode),});
  93. console.log(nodeList);
  94. NowNode.style.backgroundColor = "#02F78E";
  95. style = "#02F78E";
  96. });
  97. //清除选择项
  98. function clear()
  99. {
  100. console.log(nodeList);
  101. //如果最后停留的元素被选中,则调整此元素的style为原始style,否则不进行调整
  102. for(node of nodeList){
  103. node["node"].style.backgroundColor = node["bgcolor"];
  104. if (NowNode == node["node"]){
  105. style = node["bgcolor"];
  106. }
  107. }
  108. nodeList.splice(0,nodeList.length); //清空数组
  109. }
  110. </script> -->
  111. </html>