Home > Article > Web Front-end > Automatically generate n DIV text blocks. How do I know which DIV I clicked in the onclick event? _html/css_WEB-ITnose
DIV INDEX
The bottom line is how to bind an INDEX to each DIV when generating the DIV. I looked at it and it seems that it does not open an attribute to allow us to save the data ourselves. By the way, don’t think about innerHTML and innerText.
I originally read a bunch of products from the database and expressed them in the form of a list. Each row of this list is a DIV. After selecting a product, I need to know which row is selected in the background, and then based on INDEX Go to the product table to find the corresponding stuff.
You can set an attribute yourself, such as:
This requirement is very simple. Simply put, it is a query A bunch of data, and then display the list on the page. The list is made of div
. At this time, when you forEach traverse to generate the div, add an onclick="findById (product id)" event to the div,
and then write a function findById in js. (id) {alert(id);} Just do what you want here
div.onclick = function(event) {
event.target; // this is the target of click event
}