简体   繁体   English

我正在学习如何将 javascript 转换为 jquery,但无法通过全局变量进行正确转换

[英]I am learning how to convert javascript to jquery but can't get past the global variables to convert properly

Here is an excerpt of the original code in javascript.这是javascript中原始代码的摘录。 For the global variables, how would I convert those to jquery so I am able to resuse them later in the code, as I continue to convert it all over to jquery?对于全局变量,我如何将它们转换为 jquery,以便稍后在代码中重新使用它们,因为我继续将其全部转换为 jquery?

/* global variables tracking status of each form section */
var acresComplete = true;
var cropsComplete = true;
var monthsComplete = true;
var fuelComplete = true;

/* global variables referencing sidebar h2 and p elements */


var messageHeadElement = document.getElementById("messageHead");
var messageElement = document.getElementById("message");

/* global variables referencing fieldset elements */
var acresFieldset = document.getElementsByTagName("fieldset")[0];
var cropsFieldset = document.getElementsByTagName("fieldset")[1];
var monthsFieldset = document.getElementsByTagName("fieldset")[2];
var fuelFieldset = document.getElementsByTagName("fieldset")[3];

/* global variables referencing text input elements */
var monthsBox = document.getElementById("months");
var acresBox = document.getElementById("acres");

/* verify acres text box entry is a positive number */
function verifyAcres() {
   var validity = true;
   var messageText = "";
   try {
      if (!(acresBox.value > 0)) {
         throw "Please enter a number of acres greater than 0.";
      } 
   }
   catch(message) {
      validity = false;
      messageText = message;
      acresBox.value = ""; // remove erroneous entry from input box
   }
   finally {
      acresComplete = validity;
      messageElement.innerHTML = messageText;
      messageHeadElement.innerHTML = ""; // remove any former recommendation heading
      testFormCompleteness();      
   }
}

To select elements using ids, you can use $('#some-id') .要使用 id 选择元素,您可以使用$('#some-id') In your code, it would be在您的代码中,它将是

const messageHeadElement = $('#messageHead');
const messageElement = $('#message');

As for the fieldset s, you can use the tag selector like so:对于fieldset s,您可以像这样使用标签选择器:

const fieldsets = $('fieldset');

//.eq(0) returns a jQuery object while [0] will return the DOM element.
const acresFieldset = fieldsets.eq(0);

Nevermind, I figured out how to do what I needed to do.没关系,我想出了如何做我需要做的事情。 I needed to just delete the global variables and then go to each reference of the now deleted global variable and replacec that with one line of jquery to reference the change I need to make.我只需要删除全局变量,然后转到现在删除的全局变量的每个引用,并用一行 jquery 替换它以引用我需要进行的更改。 In the below snippet, I have deleted the variables for the sidebar elements and have made the jquery code work without a global variable.在下面的代码片段中,我删除了侧边栏元素的变量,并使 jquery 代码在没有全局变量的情况下工作。

/* global variables tracking status of each form section */
var acresComplete = true;
var cropsComplete = true;
var monthsComplete = true;
var fuelComplete = true;


/* global variables referencing fieldset elements */
var acresFieldset = document.getElementsByTagName("fieldset")[0];
var cropsFieldset = document.getElementsByTagName("fieldset")[1];
var monthsFieldset = document.getElementsByTagName("fieldset")[2];
var fuelFieldset = document.getElementsByTagName("fieldset")[3];

/* global variables referencing text input elements */
var monthsBox = document.getElementById("months");
var acresBox = document.getElementById("acres");

/* verify acres text box entry is a positive number */
function verifyAcres() {
   var validity = true;
   var messageText = "";
   try {
      if (!(acresBox.value > 0)) {
         throw "Please enter a number of acres greater than 0.";
      } 
   }
   catch(message) {
      validity = false;
      messageText = message;
      acresBox.value = ""; // remove erroneous entry from input box
   }
   finally {
      acresComplete = validity;
      $("#message").text(messageText);
      $("#messageHead").text("");
      testFormCompleteness();      
   }
}

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

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