我是学习jQuery javascript的程序员,但从未真正掌握过vanilla javascript(我知道我是一个顽皮的程序员).我的问题是如何在vanilla JS中复制这个功能? $('select').change(function() { if($(this).val() == "Ot
$('select').change(function() { if($(this).val() == "Other (please specify)") { $(this).parent().parent().find("input.hidden").show(); } });This site might help !
但这是一步一步的转换:
var selects = document.getElementsByTagName('select'); for (var i=0; i<selects.length; i++) { selects[i].onchange = function() { if( this.value == "Other (please specify)") { var elements = this.parentNode.parentNode.getElementsByTagName("input"); for (var j=0; j<elements.length; j++) { if( !elements[j].className.match(/\bhidden\b/)) continue; elements[j].style.display = ''; // the exact thing to do here would depend on your previous actions } } } }