2016-08-07 4 views
1

У меня возникла проблема с преобразованием кода из JavaScript в jQuery. Это пример кода из W3Schools, чтобы создать кнопку выпадающего списка.Преобразование с JavaScript в

// Get the button, and when the user clicks on it, execute myFunction 
document.getElementById("myBtn").onclick = function() {myFunction()}; 

/* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */ 
function myFunction() { 
    document.getElementById("myDropdown").classList.toggle("show"); 
} 

// Close the dropdown if the user clicks outside of it 
window.onclick = function(event) { 
    if (!event.target.matches('.dropbtn')) { 

    var dropdowns = document.getElementsByClassName("dropdown-content"); 
    var i; 
    for (i = 0; i < dropdowns.length; i++) { 
     var openDropdown = dropdowns[i]; 
     if (openDropdown.classList.contains('show')) { 
     openDropdown.classList.remove('show'); 
     } 
    } 
    } 
} 
+0

Загрузите это для скрипки –

+0

@ AleksandarĐokić Имеется опция фрагмента. Пожалуйста, продвигайте это. –

ответ

1

код очень просто понять, если вы try jQuery.

// Execute the code when the document is ready. 
$(document).ready(function() { 
    /* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */ 
    function myFunction() { 
    // This one removes the class "show" from the matched set of elements. 
    $(".dropdown-content").removeClass("show"); 
    // This one toggles the class "show" on the matched set of elements. 
    $("#myDropdown").toggleClass("show"); 
    } 

    // Get the button, and when the user clicks on it, execute myFunction 
    $("#myBtn").click(myFunction); 
}); 

Я не думаю, что это проблема с преобразованием кода. Я твердо верю, что W3Schools дал вам неправильный способ использования повторяющихся значений id.

Смежные вопросы