Get the DOM path of the clicked

后端 未结 8 1456
刺人心
刺人心 2020-11-30 06:01

HTML




         


        
8条回答
  •  余生分开走
    2020-11-30 06:20

        var obj = $('#show-editor-button'),
           path = '';
        while (typeof obj.prop('tagName') != "undefined"){
            if (obj.attr('class')){
                path = '.'+obj.attr('class').replace(/\s/g , ".") + path;
            }
            if (obj.attr('id')){
                path = '#'+obj.attr('id') + path;
            }
            path = ' ' +obj.prop('tagName').toLowerCase() + path;
            obj = obj.parent();
        }
        console.log(path);
    

提交回复
热议问题