E
E
Egor Mikheev2015-09-07 15:06:38
C++ / C#
Egor Mikheev, 2015-09-07 15:06:38

How to get the attributes of an element found among elements with the same class?

Hello, I ran into a problem and have not yet been able to "beautifully bypass" it.
There are HTML elements

<ul>
                    <li><a href="events" class="btn-menu">1</a></li>
                    <li><a href="about" class="btn-menu">2</a></li>
                    <li><a href="gallery" class="btn-menu">3</a></li>
                    <li><a href="connection" class="btn-menu">4</a></li>
                    <li><a href="teachers" class="btn-menu">5</a></li>
                    <li><a href="cooperations" class="btn-menu">6</a></li>
</ul>


There is a handler that should receive href

jQuery(document).ready(function($) {
    $('.btn-menu').on('click', function(event) {
        event.preventDefault();
// получаем адрес для загрузки. 
var href = './a_vie/content/' + $(this).attr('href') + '.php';
        
// закрываем меню
      var e = jQuery.Event( "click" );
      $( ".cd-close" ).trigger( e );
 
      $('#modal-about').modal({
          remote: href
      });
    });
});


Actually, when accessing the current element:
// get the address to load.
var href = './a_vie/content/' + $(this).attr('href') + '.php';
Gets the data of the first item from the btn-menu class. How to solve the problem of parsing attributes of the current element of the array without a significant increase in code

Answer the question

In order to leave comments, you need to log in

6 answer(s)
R
res2001, 2019-03-31
@elothewizz

Read immediately with a large block into an array, then process the array. And so on until the end of the file.
Buffered input in the standard library does the same thing, but if you embed it in your code, you can still save a little. If you allocate an array of a large enough size, then it may be possible to process the file in one read operation.

R
Ronald McDonald, 2019-03-31
@Zoominger

In the first while, the if condition is superfluous, this is the first thing you come across.

R
Rsa97, 2019-03-31
@Rsa97

int count = 0;
int possible = 0;
int loop = 1;
while (loop) {
  switch (getchar()) {
    case '<':
      possible = 1;
      break;
    case '>':
      if (possible) {
        count++;
      }
      possible = 0;
      break;
    case '*':
      possible = 0;
      break;
    default:
      loop = 0;
      break;
  }
}

D
disputant, 2019-04-16
@disputant

Why all these long arrays? just looking for <> pairs:

int count = 0;
    for(char last = 0, c = getchar(); c != '\n'; c = getchar())
    {
        if (last == '<' && c == '>') ++count;
        last = c;
    }
    printf("%d\n",count);

E
Egor Mikheev, 2015-09-07
@ogregor

In fact, the problem is in modal windows that load content asynchronously. (In case there are several callers attached to the same modal window class)
Before initializing the output of a new modal window, clear the cache:
$('#modal').removeData();
Or ban at a higher level.
$.ajaxSetup({
cache: false
})

S
Sergey, 2015-09-07
@frost18

Try to replace $(this) with $(event.target);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question