提问者:小点点

我的for循环在JavaScript中不工作,我怀疑for循环主体中的i<getlengthLi()函数坏了,但是为什么呢?


我的for循环在JavaScript中不工作,我怀疑我的; getlengthLi()函数中断。 我自己测试了getLengthLI函数,它返回了一个数字,所以它可以工作,但是当我在for循环中使用它时,它会中断,为什么呢?

这是我的代码

null

function getLengthLi() {
  var e = document.querySelectorAll('#list > li').length;
  return e;
}

function checkTicks() {
  var Text = ['text0', 'text1', 'text2', 'text3', 'text4', 'text5', 'text6', 'text7', 'text8', 'text9', 'text10', ]
  for (i = 1; i < getLengthLi(); i++) {
    document.write(Text[i]);
  }
}
<center>
  <div id="menu">
    <img src="images/logo.png" alt="Unisa Logo"></img>
    <br>
    <h1>Page 6</h1>
    <h2>GENERAL PROCEDURES AT THE HEARING OF A CHARGE OF MISCONDUCT
      <h2>

        <ol id="list">
          <input type="checkbox" name="text1" id="text1">
          <li>In the interest of transparency, all hearings are open meetings unless the Chairperson of the SDC is persuaded by the circumstances of the case to direct that the hearing be closed to the University community</li>
          <input type="checkbox" name="text2" id="text2">
          <li>The initiator leads evidence against the accused student and generally conducts the case for the University</li>
          <input type="checkbox" name="text3" id="text3">
          <li>The SDC allows the student or such person representing the student a reasonable opportunity to present a defence and to answer the charges</li>
          <input type="checkbox" name="text4" id="text4">
          <li>Both the initiator and the accused student are allowed to adduce all relevant evidence and call witnesses and to examine and cross-examine witnesses, as appropriate</li>
          <input type="checkbox" name="text5" id="text5">
          <li>The SDC may also ask the witnesses questions for clarity</li>
          <input type="checkbox" name="text6" id="text6">
          <li>The SDC may further, of its own accord, call for evidence that it may deem relevant to determine the issue(s) before it</li>
          <input type="checkbox" name="text7" id="text7">
          <li>The hearing of the SDC is conducted in an informal manner, according to the principles of natural justice and with due regard for the rights of the accused student. No accused student will be prejudiced by reason of a failure to comply with
            the rules of procedure or rules of evidence as applied in the ordinary courts</li>
          <input type="checkbox" name="text8" id="text8">
          <li>If the student is a minor, no disciplinary action(s) will be taken against that student before his/her parent or guardian has also been informed in writing of his/her alleged misconduct and has been given a proper opportunity to make a written
            statement and, if he/she so wishes, to appear before the SDC in person</li>
        </ol>





  </div>
  <a onclick="checkTicks()">Previous Page</a>
  <a onclick="checkTicks()">Next Page</a>
  <br>
  <footer>
    <p id="footer">©Copyright 2020</p>
  </footer>

null

我在控制台中没有得到任何结果,屏幕上也没有显示任何内容,但是,当我使用时,我; 6那么它就能工作,所以我的getLengthLI函数就是问题所在。


共3个答案

匿名用户

请检查一下它是否对你有用

null

function getLengthLi(){
    var e = document.querySelectorAll('#list > li').length;
    return e;
}

function checkTicks(){
    var Text = ['text0','text1','text2','text3','text4','text5','text6','text7','text8','text9','text10',]
    for(i = 0;i < getLengthLi();i++){
        document.write(","+Text[i]);
    }
}
checkTicks();
<ul id="list" style="list-style: none;">
<li></li>
<li></li>
<li></li>
</ul>

匿名用户

This is my HTML code

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <meta name="viewport" content="width=page-width, initial-scale=1.0">
   <title>GENERAL PROCEDURES AT THE HEARING OF A CHARGE OF MISCONDUCT</title>
   <link rel="stylesheet" href="textpages.css" />
   <script src="page6.js"></script>

</head>
<body>
<center>
<div id="menu">
    <img src="images/logo.png" alt="Unisa Logo"></img>
    <br>
    <h1>Page 6</h1>
    <h2>GENERAL PROCEDURES AT THE HEARING OF A CHARGE OF MISCONDUCT<h2>

    <ol id="list">
        <input type="checkbox" name="text1" id="text1"><li>In the interest of transparency, all hearings are open meetings unless the Chairperson of the SDC is persuaded by the circumstances of the case to direct that the hearing be closed to the University community</li>
        <input type="checkbox" name="text2" id="text2"><li>The initiator leads evidence against the accused student and generally conducts the case for the University</li>
        <input type="checkbox" name="text3" id="text3"><li>The SDC allows the student or such person representing the student a reasonable opportunity to present a defence and to answer the charges</li>
        <input type="checkbox" name="text4" id="text4"><li>Both the initiator and the accused student are allowed to adduce all relevant evidence and call witnesses and to examine and cross-examine witnesses, as appropriate</li>
        <input type="checkbox" name="text5" id="text5"><li>The SDC may also ask the witnesses questions for clarity</li>
        <input type="checkbox" name="text6" id="text6"><li>The SDC may further, of its own accord, call for evidence that it may deem relevant to determine the issue(s) before it</li>
        <input type="checkbox" name="text7" id="text7"><li>The hearing of the SDC is conducted in an informal manner, according to the principles of natural justice and with due regard for the rights of the accused student.  No accused student will be prejudiced by reason of a failure to comply with the rules of procedure or rules of evidence as applied in the ordinary courts</li>
        <input type="checkbox" name="text8" id="text8"><li>If the student is a minor, no disciplinary action(s) will be taken against that student before his/her parent or guardian has also been informed in writing of his/her alleged misconduct and has been given a proper opportunity to make a written statement and, if he/she so wishes, to appear before the SDC in person</li>
    </ol>





</div>
<a onclick="checkTicks()">Previous Page</a>
<a onclick="checkTicks()">Next Page</a>
<br>
<footer>
  <p id="footer">©Copyright 2020</p>
</footer>

匿名用户

null

function getLengthLi(){
	var e = document.querySelectorAll('#list > li').length;
	console.log(e);
	return e;
}


function checkTicks(){
    var Text = ['text0','text1','text2','text3','text4','text5','text6','text7','text8','text9','text10',]
    for(i = 0;i < getLengthLi();i++){
        document.write(","+Text[i]);
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <meta name="viewport" content="width=page-width, initial-scale=1.0">
   <title>GENERAL PROCEDURES AT THE HEARING OF A CHARGE OF MISCONDUCT</title>
   <link rel="stylesheet" href="textpages.css" />
   <script src="page6.js"></script>

</head>
<body>
<center>
<div id="menu">
	<img src="images/logo.png" alt="Logo"></img>
	<br>
	<h1>Page 6</h1>
	<h2>GENERAL PROCEDURES AT THE HEARING OF A CHARGE OF MISCONDUCT<h2>
	
	<ol id="list" style="list-style: none;">
		<input type="checkbox" name="text1" id="text1"><li>In the interest of transparency, all hearings are open meetings unless the Chairperson of the SDC is persuaded by the circumstances of the case to direct that the hearing be closed to the University community</li>
		<input type="checkbox" name="text2" id="text2"><li>The initiator leads evidence against the accused student and generally conducts the case for the University</li>
		<input type="checkbox" name="text3" id="text3"><li>The SDC allows the student or such person representing the student a reasonable opportunity to present a defence and to answer the charges</li>
		<input type="checkbox" name="text4" id="text4"><li>Both the initiator and the accused student are allowed to adduce all relevant evidence and call witnesses and to examine and cross-examine witnesses, as appropriate</li>
		<input type="checkbox" name="text5" id="text5"><li>The SDC may also ask the witnesses questions for clarity</li>
		<input type="checkbox" name="text6" id="text6"><li>The SDC may further, of its own accord, call for evidence that it may deem relevant to determine the issue(s) before it</li>
		<input type="checkbox" name="text7" id="text7"><li>The hearing of the SDC is conducted in an informal manner, according to the principles of natural justice and with due regard for the rights of the accused student.  No accused student will be prejudiced by reason of a failure to comply with the rules of procedure or rules of evidence as applied in the ordinary courts</li>
		<input type="checkbox" name="text8" id="text8"><li>If the student is a minor, no disciplinary action(s) will be taken against that student before his/her parent or guardian has also been informed in writing of his/her alleged misconduct and has been given a proper opportunity to make a written statement and, if he/she so wishes, to appear before the SDC in person</li>
	</ol>

	
	
	
	
</div>
<a onclick="checkTicks()">Previous Page</a>
<a onclick="checkTicks()">Next Page</a>
<br>
<footer>
  <p id="footer">©Copyright 2020</p>
</footer>