JavaScript gets the XPath of the element

Time:2019-12-24
//Get XPath
function readXPath(element) {
    If (element. ID! = = "") {// judge the ID attribute. If the element has an ID, the form content of // * [@ id = "XPath"] will be displayed
        return '//*[@id=\"' + element.id + '\"]';
    }
    //The main problem of string translation is needed here. Please refer to string and variable translation when JS dynamically generates HTML (note the function of quotation marks)
    If (element = = document. Body) {// recurse to the body to end the recursion
        return '/html/' + element.tagName.toLowerCase();
    }
    Var IX = 1, // position in NodeList, and initialize every time you click
         Siblings = element.parentnode.childnodes; // child elements of the same level
 
    for (var i = 0, l = siblings.length; i < l; i++) {
        var sibling = siblings[i];
        //If this element is an element in the siblings array, recursion is performed
        if (sibling == element) {
            return arguments.callee(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix) + ']';
            //If not, judge whether it is an element element, and whether it is the same element. If it is the same, start to accumulate
        } else if (sibling.nodeType == 1 && sibling.tagName == element.tagName) {
            ix++;
        }
    }
};

 

Recommended Today

Fourth, handwritten SpringMVC framework, business layer – what is coupling/dependency? How to solve

4. Business Layer 4.1 MVC model MVC:Model(Model),View(view),Controller(controller) View layer: an interface for displaying data and interacting with users=>jsp Control layer: It can accept the client's request and forward the request. The specific business function still needs to be completed with the help of the model layer component. CoreServlet  => DispacherServlet + EmpController Model layer: There are […]