简体   繁体   English

在 JS 中获取被点击元素的 ID

[英]Get ID Of Clicked Element in JS

I am trying to build a website and I ran into a problem I have this Bootstrap Carousel with different products and when they are clicked they should redirect to the page with that particular product so I am trying to make it but since every carousel item has different stuff it is kinda hard to write and change the links.我正在尝试建立一个网站,但遇到了一个问题,我有这个带有不同产品的 Bootstrap Carousel,当它们被点击时,它们应该重定向到具有该特定产品的页面,所以我正在尝试制作它,但因为每个轮播项目都有不同东西有点难以编写和更改链接。 SO I thought of surfing the net with this problem, but failed to find any answers if anybody knows the answer please consider helping me....所以我想用这个问题上网,但没有找到任何答案,如果有人知道答案,请考虑帮助我....

In the handler, use:在处理程序中,使用:

event.target.id

or

this.id

Example:例子:

<img id="product1" src="product1.png" onclick="toProductPage(event)"/>
<script>
   function toProductPage(event){
      location="product.php?id="+event.target.id;
   }
</script>

or in a shorter form:或以更短的形式:

    <img id="product1" src="product1.png" onclick="location='product.php?id='+this.id"/>

Note that 'event' can be a global variable.请注意,“事件”可以是全局变量。

So in order to keep track of what items are coughed you have to add what's called an "event listener", think of it like asking someone to wait and "listen" for a particular thing to happen, and when it does, then do something因此,为了跟踪咳嗽的项目,您必须添加所谓的“事件侦听器”,将其想象为要求某人等待并“侦听”特定事情的发生,当它发生时,然后做一些事情

That's what we need to set up, an event "listener" that waits for a particular button/buttons to be "clicked", and when they are clicked, do something这就是我们需要设置的,一个等待特定按钮/按钮被“点击”的事件“监听器”,当它们被点击时,做一些事情

To do this you need a reference of your HTML button in JavaScripts, one way to do this is to give the element an ID, then that ID can be accessed directly as a global variable in JavaScript, which let's you and this "event listener" to the particular button, to wait for it to be clicked为此,您需要在 JavaScript 中引用您的 HTML 按钮,一种方法是为元素提供一个 ID,然后该 ID 可以作为 JavaScript 中的全局变量直接访问,这让您和这个“事件侦听器”到特定按钮,等待它被点击

Then when it is clicked, we can get all the information of the button we want, or do any kind if action that JavaScripts can do然后当它被点击时,我们可以得到我们想要的按钮的所有信息,或者做任何 JavaScript 可以做的动作

So所以

<Button id="hi"></button>
<Script>
hi.addEventListener("click", function (event) {
    console.log("clicked this button!!! ", event)
})
</Script>

Then check the console for more information on the event.然后检查控制台以获取有关该事件的更多信息。 Obviously you can also add these event listener from an array of buttons with the same class if you loop through the array and add it to each one, just call .显然,如果您遍历数组并将其添加到每个按钮中,您也可以从具有相同类的按钮数组中添加这些事件侦听器,只需调用 . addEventListener to each button object and you're good to go, all the information and attributes can be accessed from the event object, either event.target, event.srcElement etc, just see the console for all the properties addEventListener 到每个按钮对象,你就可以开始了,所有的信息和属性都可以从事件对象中访问,无论是 event.target、event.srcElement 等,只需查看所有属性的控制台

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM