简体   繁体   English

我如何把这个JavaScript变成jQuery

[英]How do I turn this javascript into jQuery

I am customising this W3Schools tutorial . 我正在自定义W3Schools教程 I would like to use jQuery to add animation to the transition between tabs. 我想使用jQuery向选项卡之间的过渡添加动画。 This is the relevant line in javascript function 这是javascript函数中的相关行

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

I have tried this but it doesn't work. 我已经尝试过了,但是没有用。

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

You're currently trying to get an element <xn="something"> . 您当前正在尝试获取元素<xn="something"> Simply remove the quotes so it's not a selector but a jQuery object: 只需删除引号,使其不是选择器而是jQuery对象:

function showTab(n) {
  var x = document.getElementsByClassName("tab");
  $(x[n]).show(750);
}

You need to provide the value of x[n] to the jQuery constructor, not as a string literal: 您需要将x[n]的值提供给jQuery构造函数,而不是作为字符串文字:

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

That being said if you want to fully convert this to jQuery you can use a jQuery object and the eq() method to retrieve an element within it by index: 话虽如此,如果您想将其完全转换为jQuery,则可以使用jQuery对象和eq()方法通过索引检索其中的元素:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM