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

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

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

问题

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

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

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

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

我该如何做?

  1. <div>
  2. <div>
  3. <div>
  4. <div>
  5. <button id="myButton" onclick="onclicked(event)">点击</button>
  6. </div>
  7. </div>
  8. </div>
  9. </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)

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

你可以如何获取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 -->

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

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

  1. &lt;div&gt;
  2. &lt;div&gt;
  3. &lt;div&gt;
  4. &lt;div&gt;
  5. &lt;button id=&quot;myButton&quot; onclick=&quot;onclicked(event)&quot;&gt;click&lt;/button&gt;
  6. &lt;/div&gt;
  7. &lt;/div&gt;
  8. &lt;/div&gt;
  9. &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!

  1. const log = console.log;
  2. const cache = {
  3. element: undefined,
  4. selector: undefined
  5. };
  6. function onClicked(event) {
  7. log(event.target);
  8. const element = event.target;
  9. cache.element = element;
  10. cache.selector = element.tagName.toLowerCase() + '#' + element.getAttribute('id');
  11. log(cache);
  12. }
  1. <div>
  2. <div>
  3. <div>
  4. <div>
  5. <button onclick="onClicked(event)" id="myButton">click</button>
  6. </div>
  7. </div>
  8. </div>
  9. </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 -->

  1. const log = console.log;
  2. const cache = {
  3. element: undefined,
  4. selector: undefined
  5. };
  6. function onClicked(event) {
  7. log(event.target);
  8. const element = event.target;
  9. cache.element = element;
  10. cache.selector = element.tagName.toLowerCase() + &#39;#&#39; + element.getAttribute(&#39;id&#39;);
  11. log(cache);
  12. }

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

  1. &lt;div&gt;
  2. &lt;div&gt;
  3. &lt;div&gt;
  4. &lt;div&gt;
  5. &lt;button onclick=&quot;onClicked(event)&quot; id=&quot;myButton&quot;&gt;click&lt;/button&gt;
  6. &lt;/div&gt;
  7. &lt;/div&gt;
  8. &lt;/div&gt;
  9. &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:

确定