而没有实际发生click事件

而没有实际发生click事件

本文介绍了发生onclick事件,而没有实际发生click事件-js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

民谣我是一个新手,所以我肯定会为一个非常基本的问题预先道歉.

FolksI'm a complete novice so apologise upfront for what is going to be a very basic question, I'm sure.

我有一个javascript文件,该文件链接到包含许多按钮的html文件.一个这样的按钮的onclick事件似乎完全绕过了onlick事件,并在页面打开后立即触发.有任何想法吗?不好意思地问,什么可能是最基本的东西,但是我已经失去了花费在试图使它仅在"click"事件上起作用的时间....

I have a javascript file that is linked to a html file which contains a number of buttons. The onclick event for one such button seems to be completely bypassing the onlick event and triggering as soon as the page opens. Any ideas? Sorry to ask what is likely something really basic, but I have lost count of the hours I have spent trying to get it to work only on the "click" event....

在Java中:

var arrEnrolments=[];//global array
function init() {
    populateEnrolments();
    var enrolArray=arrEnrolments

    document.getElementById("resetEnrol").onclick=resetEnrolments;
    document.getElementById("listEnrol").onclick=listAllEnrols;
   document.getElementById("sortEnrol").onclick=sortByName(arrEnrolments);
}

function populateEnrolments(){
arrEnrolments=[];
var stuOne={id:1234567, name: "Jones, Brigit", mark: null, grade: ""};
var stuTwo={id:1234566, name: "Somers, Tom ", mark: null, grade: ""};
var stuThree={id:1222445, name: "Lee, Vera", mark: null, grade: ""};
var stuFour={id:1220123, name: "Crawford, Anne", mark: null, grade: ""};
var stuFive={id:1244444, name: "Wu, Russel", mark: null, grade: ""};
var stuSix={id:1238899, name: "Shaw, Boris", mark: null, grade: ""};
var stuSeven={id:1237799, name: "Wilson, Terri", mark: null, grade: ""};
arrEnrolments=[stuOne, stuTwo, stuThree, stuFour, stuFive, stuSix, stuSeven];
}

function sortByName(searchArray) {

var pos=0;
var count, temp;

do{
    var msgOut="Student records sorted into Name ascending sequence";
    count=0;
    for(var pos=0; pos<searchArray.length-1; pos++){
        if(searchArray[pos].name>searchArray[pos+1].name){
            temp=searchArray[pos];
            searchArray[pos]=searchArray[pos+1];
            searchArray[pos+1]=temp;
            count++
        }//end if
    }//end for
} while (count>0)//end do

addToOutput(msgOut);
}

function addToOutput(output){
var msgOut=output;
document.getElementById("output").innerHTML=msgOut;

}

window.onload = init;

在html中:

<body>
<header>
<h1>Tracking Student Enrolments and Completions<h1>
</header>
<article class="flex-container">
<section class="flex-item">
<h2>Enrolments</h2>
<button type="button" id="resetEnrol">Reset Enrolment List</button>
<button type="button" id="listEnrol">List All Enrolments</button>
<button type="button" id="sortEnrol">Sort Enrolments by Name</button><br/>
</article>
</body>

推荐答案

解决方案:

document.getElementById('sortEnrol').onclick = sortByName(arrEnrolments);

没有注册eventListener,而是执行sortByName函数.您需要在此处关闭,如下所示:

is not registering an eventListener but instead executing the sortByName function.You need a closure here as follows:

document.getElementById('sortEnrol').onclick = function () { sortByName(arrEnrolments); };

document.getElementById('sortEnrol').onclick = function () { sortByName(arrEnrolments); };

一些评论:

  • enrolArray变量是在init函数中创建的,但从未使用过

  • enrolArray variable is created in the init function but never used

pos变量在sortByName函数中定义了两次

pos variable is defined twice in the sortByName function

这篇关于发生onclick事件,而没有实际发生click事件-js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:40