Return XPath location with jQuery? Need some feedback on a function

前端 未结 2 617
离开以前
离开以前 2020-12-10 22:31

For the following project I will be using PHP and jQuery.

I have the following code:

$(\'*\').onclick(function(){

});

Once a user

2条回答
  •  [愿得一人]
    2020-12-10 23:00

    a fix to Gaby aka G. Petrioli

    added "nd" to children

    .children(nd)
    

    full:

    $(document).delegate('*','click',function(){
            var path = $(this).parents().andSelf();
            var xpath=''; // firebug xpath starts with '/html'
            for (var i = 0; i < path.length; i++) {
                var nd = path[i].nodeName.toLowerCase();
                if (nd =="tbody") continue;  // php DOMxpath ignores tbody? or browser fixes html?
                xpath += '/';
                if (nd != 'html' && nd != 'body')
                {xpath += nd+'['+ ($(path[i-1]).children(nd).index(path[i])+1) +']';} //! saving time ha?
                else
                {xpath += nd;}                    
            }
            alert(xpath);
            return false;
        });
    

提交回复
热议问题