我现有的代码是javascript,我想在jquery中转换这些代码?

时间:2016-08-03 09:04:01

标签: javascript jquery html css

如何将其转换为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";
}

3 个答案:

答案 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)

使用 show() hide() 方法。

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>