T
T
TheInsable2017-07-08 18:14:29
Docker
TheInsable, 2017-07-08 18:14:29

What is the best way to attach a handler to an element?

There is a navigation, and the elements of this navigation (links) are loaded via the API from the server, an example of how this is implemented:

for (var i =  0; i < links.length; i++) {
   var li = document.createElement('li');
   li.innerHTML = links[i].name;
   $('.nav__content').append(li);
}

An li element is created and placed in a ul with the nav__content class, but a handler needs to be attached to these li so that some li can be expanded, since some links will have subcategories.
What is the best way to do this? So far, there is only one idea: add a class to these li-elements and catch them in js. But I don't think this is the best solution.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan Shumov, 2018-12-29
@inoise

The official documentation is called

R
rustacean137, 2018-12-30
@rustacean137

It doesn't have to be done through docker.
Install multiple versions of php.
In your case, install `php7.0` from the ppa repository:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.0 php7.0-fpm

Then configure your webserver to use the correct version. For example in nginx, set `fastgci_pass`:
# ...
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# ...

N
Negwereth, 2017-07-08
@TheInsable

Here it is better to hang the handler on the parent ul.
https://davidwalsh.name/event-delegate

V
Vladimir, 2017-07-08
@djQuery

In jQuery it looks like this:

$(".nav__content").on( "click", "li", function(){
  // ваш обработчик клика по любому li
  // если какой-то li должен обрабатываться специфическим образом, 
  // задайте ему класс и работайте с этим классом
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question