我会使用小书签。我有一个XPath相关,但我不知道它是否可以在IE中使用。我得走了,但是如果它在IE上可以工作,我会对其进行测试并给出。
Web开发人员从我的书签中获得的两个小书签站点:Subsimple的小书签和Squarefree的小书签。那里有很多有用的东西…
[编辑]好,我回来了。我拥有的小书签仅适用于FF,不是最佳选择。我终于重写了它,尽管使用了最初的想法。无法找到我找到它的地方。
扩展的JS:
function getNode(node){ var nodeExpr = node.tagName; if (nodeExpr == null) // Eg. node = #text return null; if (node.id != '') { nodeExpr += "[@id='" + node.id + "']"; // We don't really need to go back up to //HTML, since IDs are supposed // to be unique, so they are a good starting point. return "/" + nodeExpr; }// We don't really need this//~ if (node.className != '')//~ {//~ nodeExpr += "[@]";//~ } // Find rank of node among its type in the parent var rank = 1; var ps = node.previousSibling; while (ps != null) { if (ps.tagName == node.tagName) { rank++; } ps = ps.previousSibling; } if (rank > 1) { nodeExpr += '[' + rank + ']'; } else { // First node of its kind at this level. Are there any others? var ns = node.nextSibling; while (ns != null) { if (ns.tagName == node.tagName) { // Yes, mark it as being the first one nodeExpr += '[1]'; break; } ns = ns.nextSibling; } } return nodeExpr;}var currentNode;// Standard (?)if (window.getSelection != undefined) currentNode = window.getSelection().anchorNode;// IE (if no selection, that's BODY)else currentNode = document.selection.createRange().parentElement();if (currentNode == null){ alert("No selection"); return;}var path = [];// Walk up the Domwhile (currentNode != undefined){ var pe = getNode(currentNode); if (pe != null) { path.push(pe); if (pe.indexOf('@id') != -1) break; // Found an ID, no need to go upper, absolute path is OK } currentNode = currentNode.parentNode;}var xpath = "/" + path.reverse().join('/');alert(xpath);// Copy to clipboard// IEif (window.clipboardData) clipboardData.setData("Text", xpath);// FF's pre to handle clipboard is much more complex // and might need to change prefs to allow changing the clipboard content.// I omit it here as it isn't part of the original request.
您必须选择元素并激活小书签以获取其XPath。
现在,小书签版本(感谢Bookmarklet
Builder):
IE
(我不得不将其分为两部分,因为IE不喜欢很长的书签(最大大小取决于IE版本!)。您必须先激活第一个(功能def),然后再激活第二个。经过IE6测试)
javascript:function getNode(node){var nodeExpr=node.tagName;if(!nodeExpr)return null;if(node.id!=''){nodeExpr+="[@id='"+node.id+"']";return "/"+nodeExpr;}var rank=1;var ps=node.previousSibling;while(ps){if(ps.tagName==node.tagName){rank++;}ps=ps.previousSibling;}if(rank>1){nodeExpr+='['+rank+']';}else{var ns=node.nextSibling;while(ns){if(ns.tagName==node.tagName){nodeExpr+='[1]';break;}ns=ns.nextSibling;}}return nodeExpr;}javascript:function o__o(){var currentNode=document.selection.createRange().parentElement();var path=[];while(currentNode){var pe=getNode(currentNode);if(pe){path.push(pe);if(pe.indexOf('@id')!=-1)break;}currentNode=currentNode.parentNode;}var xpath="/"+path.reverse().join('/');clipboardData.setData("Text", xpath);}o__o();
FF
javascript:function o__o(){function getNode(node){var nodeExpr=node.tagName;if(nodeExpr==null)return null;if(node.id!=''){nodeExpr+="[@id='"+node.id+"']";return "/"+nodeExpr;}var rank=1;var ps=node.previousSibling;while(ps!=null){if(ps.tagName==node.tagName){rank++;}ps=ps.previousSibling;}if(rank>1){nodeExpr+='['+rank+']';}else{var ns=node.nextSibling;while(ns!=null){if(ns.tagName==node.tagName){nodeExpr+='[1]';break;}ns=ns.nextSibling;}}return nodeExpr;}var currentNode=window.getSelection().anchorNode;if(currentNode==null){alert("No selection");return;}var path=[];while(currentNode!=undefined){var pe=getNode(currentNode);if(pe!=null){path.push(pe);if(pe.indexOf('@id')!=-1)break;}currentNode=currentNode.parentNode;}var xpath="/"+path.reverse().join('/');alert(xpath);}o__o();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)