Get the DOM path of the clicked

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

HTML






        
相关标签:
8条回答
  • 2020-11-30 06:29

    hello this function solve the bug related to current element not show in the path

    check this now

    $j(".wrapper").click(function(event) {
          selectedElement=$j(event.target);
    
          var rightArrowParents = [];
          $j(event.target).parents().not('html,body').each(function() {
              var entry = this.tagName.toLowerCase();
              if (this.className) {
                  entry += "." + this.className.replace(/ /g, '.');
              }else if(this.id){
                  entry += "#" + this.id;
              }
              entry=replaceAll(entry,'..','.');
              rightArrowParents.push(entry);
          });
          rightArrowParents.reverse();
          //if(event.target.nodeName.toLowerCase()=="a" || event.target.nodeName.toLowerCase()=="h1"){
            var entry = event.target.nodeName.toLowerCase();
            if (event.target.className) {
                  entry += "." + event.target.className.replace(/ /g, '.');
            }else if(event.target.id){
                  entry += "#" + event.target.id;
            }
            rightArrowParents.push(entry);
         // }
    

    where $j = jQuery Variable

    also solve the issue with .. in class name

    here is replace function :

    function escapeRegExp(str) {
        return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
    }
      function replaceAll(str, find, replace) {
      return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
    }
    

    Thanks

    0 讨论(0)
  • 2020-11-30 06:36

    Here is a native JS version that returns a jQuery path. I'm also adding IDs for elements if they have them. This would give you the opportunity to do the shortest path if you see an id in the array.

    var path = getDomPath(element);
    console.log(path.join(' > '));
    

    Outputs

    body > section:eq(0) > div:eq(3) > section#content > section#firehose > div#firehoselist > article#firehose-46813651 > header > h2 > span#title-46813651
    

    Here is the function.

    function getDomPath(el) {
      var stack = [];
      while ( el.parentNode != null ) {
        console.log(el.nodeName);
        var sibCount = 0;
        var sibIndex = 0;
        for ( var i = 0; i < el.parentNode.childNodes.length; i++ ) {
          var sib = el.parentNode.childNodes[i];
          if ( sib.nodeName == el.nodeName ) {
            if ( sib === el ) {
              sibIndex = sibCount;
            }
            sibCount++;
          }
        }
        if ( el.hasAttribute('id') && el.id != '' ) {
          stack.unshift(el.nodeName.toLowerCase() + '#' + el.id);
        } else if ( sibCount > 1 ) {
          stack.unshift(el.nodeName.toLowerCase() + ':eq(' + sibIndex + ')');
        } else {
          stack.unshift(el.nodeName.toLowerCase());
        }
        el = el.parentNode;
      }
    
      return stack.slice(1); // removes the html element
    }
    
    0 讨论(0)
提交回复
热议问题