如何将此javascript转换为jQuery

问题描述 投票:-1回答:2

我正在定制这个W3Schools tutorial。我想使用jQuery将动画添加到选项卡之间的过渡。这是javascript函数中的相关行

function showTab(n) {
  // This function will display the specified tab of the form ...
  var x = document.getElementsByClassName("tab");
  x[n].style.display = "block";

我试过这个,但它不起作用。

function showTab(n) {
  // This function will display the specified tab of the form ...
  var x = document.getElementsByClassName("tab");
  $("x[n]").show(750);
}
javascript jquery html css
2个回答
2
投票

您需要将x[n]的值提供给jQuery构造函数,而不是字符串文字:

$(x[n]).show(750);

话虽如此,如果你想将它完全转换为jQuery,你可以使用jQuery对象和eq()方法通过索引检索其中的元素:

function showTab(n) {
  $('.tab').eq(n).show(750);
}

2
投票

你现在正试图获得一个元素<x n="something">。只需删除引号,这样它就不是选择器而是jQuery对象:

function showTab(n) {
  var x = document.getElementsByClassName("tab");
  $(x[n]).show(750);
}
© www.soinside.com 2019 - 2024. All rights reserved.