你可以如何获取HTML节点元素的选择器字符串?

huangapple go评论51阅读模式
英文:

how can I get the selector string of an HTML node element?

问题

基本上,我正在获取一个HTML元素。我想要做相反的事情,从这个HTML元素中,我想要获取它的选择器字符串(类似于这样:button#myButton

function onclicked(e){
 console.log(e.target);  // 我得到节点HTML元素
}

假设我不知道id或元素是什么,我只有在点击它时才会知道。

我需要从e.target中获取与屏幕截图相同的选择器字符串。

我该如何做?

<div>
  <div>
    <div>
      <div>
        <button id="myButton" onclick="onclicked(event)">点击</button>
      </div>
    </div>
  </div>
</div>
英文:

Basically, I am getting an HTML element. I want to do the inverse, from this HTML element, I want to get its selector string (something like this: button#myButton)

function onclicked(e){
 console.log(e.target);  // I get node HTML element
}

你可以如何获取HTML节点元素的选择器字符串?

suppose I don't know what the id or element is, I will only know this when I click on it.

I need to get the selector string from e.target that is the same of the screenshot.

how can I do it?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function onclicked(e){
  console.log(e.target);
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;div&gt;
    &lt;div&gt;
      &lt;div&gt;
        &lt;button id=&quot;myButton&quot; onclick=&quot;onclicked(event)&quot;&gt;click&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

I'll be completely transparent with you here, I'm not entirely sure what you're trying to achieve here. Considering you're using document.querySelector anyway, why aren't you storing the button there? Is there a problem with storing a reference to the actual DOM element as opposed to the query selector string?

I've basically written out a pseudo implementation which I believe provides an answer to your question. Despite the fact that I'm not very confident that I know what you're trying to do or why you want to do it this way.

I strongly recommend you take a look at what I've done & review it, my answer is not a work of art, rather a simple solution to the problem. By all means provide some feedback if this answer doesn't provide the outcome you were looking for. I'm happy to help if I can!

const log = console.log;

const cache = {
  element: undefined,
  selector: undefined
};

function onClicked(event) {
  log(event.target);
  const element = event.target;
  cache.element = element;
  cache.selector = element.tagName.toLowerCase() + '#' + element.getAttribute('id');
  log(cache);
}
<div>
  <div>
    <div>
      <div>
        <button onclick="onClicked(event)" id="myButton">click</button>
      </div>
    </div>
  </div>
</div>
英文:

I'll be completely transparent with you here, I'm not entirely sure what you're trying to achieve here. Considering you're using document.querySelector anyway, why aren't you storing the button there? Is there a problem with storing a reference to the actual DOM element as opposed to the query selector string?

I've basically written out a pseudo implementation which I believe provides an answer to your question. Despite the fact that I'm not very confident that I know what you're trying to do or why you want to do it this way.

I strongly recommend you take a look at what I've done & review it, my answer is not a work of art, rather a simple solution to the problem. By all means provide some feedback if this answer doesn't provide the outcome you were looking for. I'm happy to help if I can! 🙂

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const log = console.log;

const cache = {
  element: undefined,
  selector: undefined
};

function onClicked(event) {
  log(event.target);
  const element = event.target;
  cache.element = element;
  cache.selector = element.tagName.toLowerCase() + &#39;#&#39; + element.getAttribute(&#39;id&#39;);
  log(cache);
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;div&gt;
    &lt;div&gt;
      &lt;div&gt;
        &lt;button onclick=&quot;onClicked(event)&quot; id=&quot;myButton&quot;&gt;click&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 21:28:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定