最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Catch element (element which has click event bind on it) when clicked using Javascript - Stack Overflow

programmeradmin2浏览0评论

Imagine this simple html code:

<section onclick="dosomething(event)">
    <div slot="header" class="collapse-header">
      <div class="collapse-image"></div>
      <div class="collapse-text-price-container">
        <div class="collapse-text">სასაჩუქრე ბარათი 200 ლარი – ტანსაცმლის მაღაზია ZURA & SHARK</div>
        <div minprice="1200"></div>
      </div>
      <div class="center-item-container">
        <div class="status success-status">წაღებული</div>
      </div>
      <div class="center-item-container">
        <div class="date-text">10 იან, 2018</div>
      </div>
      <div class="center-item-container">
        <iron-icon class="icon" icon="bog:chevron-down"></iron-icon>
      </div>
    </div>
    <div slot="body" class="collapse-body">
      <div>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.</div>
    </div>
  </section>

Pay attention to this part of the code : onclick="dosomething(event)". as you see I have many other elements inside outer <section> tag. I also have many sections like that, and on click, I want to get the exact section element which was clicked, How can I do that? event.srcElement sometimes return element inside this section, not the section itself. So what is the solution?

My current solution (which I don't like) is that I take property path or posedPath from event, it's array of elements, and I iterate through it until I find element with tag name section.

Imagine this simple html code:

<section onclick="dosomething(event)">
    <div slot="header" class="collapse-header">
      <div class="collapse-image"></div>
      <div class="collapse-text-price-container">
        <div class="collapse-text">სასაჩუქრე ბარათი 200 ლარი – ტანსაცმლის მაღაზია ZURA & SHARK</div>
        <div minprice="1200"></div>
      </div>
      <div class="center-item-container">
        <div class="status success-status">წაღებული</div>
      </div>
      <div class="center-item-container">
        <div class="date-text">10 იან, 2018</div>
      </div>
      <div class="center-item-container">
        <iron-icon class="icon" icon="bog:chevron-down"></iron-icon>
      </div>
    </div>
    <div slot="body" class="collapse-body">
      <div>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.</div>
    </div>
  </section>

Pay attention to this part of the code : onclick="dosomething(event)". as you see I have many other elements inside outer <section> tag. I also have many sections like that, and on click, I want to get the exact section element which was clicked, How can I do that? event.srcElement sometimes return element inside this section, not the section itself. So what is the solution?

My current solution (which I don't like) is that I take property path or posedPath from event, it's array of elements, and I iterate through it until I find element with tag name section.

Share Improve this question edited Aug 16, 2019 at 10:39 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 16, 2019 at 10:06 O. ShekriladzeO. Shekriladze 1,5463 gold badges23 silver badges47 bronze badges 1
  • 1. .srcElement is a proprietary property introduced by Microsoft in their global event object. The "standardized" way would be to use .addEventListener() which passes a Event object as the first argument into the registered event handler. Then use the standardized target property of it. 2. Have a look at Element.closest() – Andreas Commented Aug 16, 2019 at 10:13
Add a ment  | 

5 Answers 5

Reset to default 5

Try to change function invoking line like that:

<section onclick="dosomething(this, event)">

and then function:

function dosomething(me, e) {
    console.log(me); // returns the section, which was clicked
    console.log(e.target); // returns the element, which was clicked
}

You could use event.target and pass this from the event to get the source element:

function dosomething(event, source) {
  console.log(source.tagName);
  event.target.classList.add('red');
}
.red {
  color: red;
}
<section onclick="dosomething(event, this)">
  <div slot="header" class="collapse-header">
    <div class="collapse-image"></div>
    <div class="collapse-text-price-container">
      <div class="collapse-text">სასაჩუქრე ბარათი 200 ლარი – ტანსაცმლის მაღაზია ZURA & SHARK</div>
      <div minprice="1200"></div>
    </div>
    <div class="center-item-container">
      <div class="status success-status">წაღებული</div>
    </div>
    <div class="center-item-container">
      <div class="date-text">10 იან, 2018</div>
    </div>
    <div class="center-item-container">
      <iron-icon class="icon" icon="bog:chevron-down"></iron-icon>
    </div>
  </div>
  <div slot="body" class="collapse-body">
    <div>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.</div>
  </div>
</section>

You can use event.target

function dosomething(event) {
  console.log(event.target)
}
<section onclick="dosomething(event)">
    <div slot="header" class="collapse-header">
      <div class="collapse-image"></div>
      <div class="collapse-text-price-container">
        <div class="collapse-text">სასაჩუქრე ბარათი 200 ლარი – ტანსაცმლის მაღაზია ZURA & SHARK</div>
        <div minprice="1200"></div>
      </div>
      <div class="center-item-container">
        <div class="status success-status">წაღებული</div>
      </div>
      <div class="center-item-container">
        <div class="date-text">10 იან, 2018</div>
      </div>
      <div class="center-item-container">
        <iron-icon class="icon" icon="bog:chevron-down"></iron-icon>
      </div>
    </div>
    <div slot="body" class="collapse-body">
      <div>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.</div>
    </div>
  </section>

Or if you just want to get the source section, you just need to pass this to the function.

function dosomething(section) {
  console.log(section)
}
<section onclick="dosomething(this)">
    <div slot="header" class="collapse-header">
      <div class="collapse-image"></div>
      <div class="collapse-text-price-container">
        <div class="collapse-text">სასაჩუქრე ბარათი 200 ლარი – ტანსაცმლის მაღაზია ZURA & SHARK</div>
        <div minprice="1200"></div>
      </div>
      <div class="center-item-container">
        <div class="status success-status">წაღებული</div>
      </div>
      <div class="center-item-container">
        <div class="date-text">10 იან, 2018</div>
      </div>
      <div class="center-item-container">
        <iron-icon class="icon" icon="bog:chevron-down"></iron-icon>
      </div>
    </div>
    <div slot="body" class="collapse-body">
      <div>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.</div>
    </div>
  </section>

You can use this as a parameter and you can get all child elements with "this". For example change below html text: to

And in javascript you can do like this:

function dosomething(event,this) 
{
    var _this = this; // get section element
    var _elem = event.target // get self element
    var childElements = _this.childNodes; //get child elements

    /* if you are using jQuery do like this */
    var xxxElem = $(_this).find(".xxx");
}

You can check class or any specific properties where it is clicked

function (event){

const ele = event.target; }

发布评论

评论列表(0)

  1. 暂无评论