如何将其转换为jQuery?
openDish("balkandish1")
function openDish(dishName) {
var i;
var x = document.getElementsByClassName("city");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
document.getElementById(dishName).style.display = "block";
}
答案 0 :(得分:3)
您只需要在相关选择器上调用hide()
和show()
,如下所示:
openDish("balkandish1")
function openDish(dishName) {
$(".city").hide()
$('#' + dishName).show();
}
答案 1 :(得分:2)
jQuery提供.show()
和.hide()
函数,可用于隐藏和显示DOM元素。
openDish("balkandish1");
function openDish(dishName) {
$(".city").hide();
$('#'+ dishName).show();
}
答案 2 :(得分:2)
openDish("balkandish1");
function openDish(dishName) {
// get all elements using class selector
// hide them using `hide()` method
$(".city").hide();
// get the element using `id` selector
// make it visible using `show()` method
$('#' + dishName).show();
}
openDish("balkandish1");
function openDish(dishName) {
$(".city").hide();
$('#' + dishName).show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="city">1</div>
<div class="city">2</div>
<div class="city">3</div>
<div class="city" id="balkandish1">4</div>
<div class="city">5</div>
<div class="city">6</div>