Automatically generate n DIV text blocks. How do I know which DIV I clicked in the onclick event? _html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:21:23
Original
1304 people have browsed it

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.
The way I think of now is to take the names from ID0 to IDn on the ID. When the time comes, judge based on the ID.
Is there a better way?

Reply to the discussion (solution)

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.





Untitled Document






Try something like this



By default, xml can add any attribute

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
}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template