How to write .level + 1 as a selector in jQuery or JavaScript?

Quick question. I have a DIV series with the following classes.level1 .level2 .level3

This is what I want ... When I'm over .level1, I would like the selector to select .level2. In other words, .level+1 everything that I would point.

How to write this in JavaScript or jQuery ... X in code, where I need this to go

$(".no-submenu").hover(function() {
  $.data(this, "timer", setTimeout($.proxy(function() {

    $( X ).hide();

  }, this), 700));
}, function() {
  clearTimeout($.data(this, "timer"));
});

Any help is much appreciated, thanks

+3
source share
2 answers

If they are ok, you can use . next () to begin a selection attribute

$(this).next('[class^=level]').hide();

JsFiddle example


You can perform a replacement using the function:

edit: +

function replacer(num)  {
    return (parseInt(num, 10) + 1);
}
var selector = $(this).attr("class").replace(/([\d]+)$/,replacer);    
$("." + selector).hide();

, X = "." + selector

jsFiddle


. replace()
. parseInt()

... DIV . divs, jQuery . Eq().

+1

, ,

var x = 1; // You are probably going to change this
$('.level'+x).hover(function() {
$('.level'+(x+1)).dosomething();
},function () {
$('.level'+(x+1)).dosomethingelse();
});
0

Source: https://habr.com/ru/post/1764687/


All Articles