I have code that, when clicked, expands a drop-down list to show a set of links. There are three different drop-down lists with different sets of links that show when the user clicks.
The script is below, but it is not dynamic. What is the best and most efficient way to write a script, so I don’t need to add it when additional dropdowns are needed?
$(".button-nav1").click(function() { $(".row1").toggle(); }); $(".button-nav2").click(function() { $(".row2").toggle(); }); $(".button-nav3").click(function() { $(".row3").toggle(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div id="row1" class="button-nav1">Joe<span class="crt"> ▼</span> </div> <div class="row1 BGpadding"> <ul class="2ndRESET"> <li class="resetTWO"><strong>Overview</strong> </li> <li><strong>Training</strong> </li> <ul class="resetTWO"> <li><a href="#">Lorem Lipsum</a> </li> </ul> <li><strong>Toomy</strong> </li> <ul class="resetTWO"> <li><a href="#">Lipsum </a> </li> <li><a href="#">Loremu</a> </li> <li><a href="#">Lipsum</a> </li> </ul> </ul> </div> </div> <div id="row2" class="button-nav2">Joe<span class="crt"> ▼</span> </div> <div class="row2 BGpadding"> <ul class="2ndRESET"> <li class="resetTWO"><strong>Overview</strong> </li> <li><strong>Training and Help</strong> </li> <ul class="resetTWO"> <li><a href="#">Lorem Lipsum</a> </li> </ul> <li><strong>Toomy</strong> </li> <ul class="resetTWO"> <li><a href="#">Lipsum </a> </li> <li><a href="#">Loremu</a> </li> <li><a href="#">Lipsum</a> </li> </ul> </ul> </div> </div> <div id="row3" class="button-nav3">Joe<span class="crt"> ▼</span> </div> <div class="row3 BGpadding"> <ul class="2ndRESET"> <li class="resetTWO"><strong>Overview</strong> </li> <li><strong>Training</strong> </li> <ul class="resetTWO"> <li><a href="#">Lorem Lipsum</a> </li> </ul> <li><strong>Toomy</strong> </li> <ul class="resetTWO"> <li><a href="#">Lipsum </a> </li> <li><a href="#">Loremu</a> </li> <li><a href="#">Lipsum</a> </li> </ul> </ul> </div> </div>
jquery . div, , button-nav $("div[class^=button-nav]"). id id $("."+$(this).attr('id')).toggle();
div
button-nav
$("div[class^=button-nav]")
id
$("."+$(this).attr('id')).toggle();
$("div[class^=button-nav]").on('click',function() { $("."+$(this).attr('id')).toggle(); });
, .row1 . , #row1. ,
.row1
#row1
$('.button-nav1,.button-nav2,.button-nav3').click(function(){ $(this).toggle(); }
, .button-nav1, .button-nav2 .button-nav3 ( button-nav)
.button-nav1
.button-nav2
.button-nav3
$(document).on('click', '.button-nav', function(){ $(this).toggle(); }
Source: https://habr.com/ru/post/1652622/More articles:Why is there no std :: from_string ()? - c ++Конкатенация строки и char литералов в C - cMultidexed apk test will not work on api-19 - androidFind the correlation between columns whose names are specified as values in another data frame - rПоказать Модифицированное окно Bootstrap на основе значения select (DropDown) - javascripthttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1652623/how-to-convert-g-commands-to-cmakeliststxt&usg=ALkJrhgoq05k0fB69fWXWu8uEXZEIqRkBgpandas: all NaN when subtracting two data frames - pythonHow to redirect System.out to a log file using Logback? - javaAzure Append Blobs slows read performance - azureWhy are these sampling methods asynchronous? - javascriptAll Articles