为什么此addEvent函数不起作用?

时间:2019-07-19 07:55:52

标签: javascript html

我是JS技术的新手。我以Javascript圣经为起点。尽管有些过时的主题和不推荐使用的功能,但那本书使我深入了解了JS行为。特定的代码块对我不起作用。

我真的不知道“这是否是已弃用的功能或浏览器问题”,我已经在所有资源中搜索了addEvent。现在大多数人都使用addEventListener

我只需要知道区别。如何解决此代码?

返回错误“未捕获的ReferenceError:未定义addEvent”的原因是什么。我需要在哪里定义此addEvent?

// **jsb-23-02.js**

// initialize when the page has loaded
addEvent(window, "load", testValues);
var newElem;
var newText;
var toyGlobal = "Gumby";
var aBoy = "Charlie Brown";
var hisDog = "Snoopy";

function showLocal() {
  var toyLocal = "Pokey";
  return toyLocal;
}

function showGlobal() {
  newElem = document.createElement("div");
  newElem.className = "objProperty";
  newText = document.createTextNode("Global version of hisDog is intact: " +
    hisDog);
  // just picked up a global variable value instead
  // of the local variable in the calling function

  newElem.appendChild(newText);
  placeHolderElement.appendChild(newElem);
}

function testValues() {
  // Dangerous ground here -- declaring a global variable in a function
  placeHolderElement = document.getElementById("placeHolder");
  // Now declare the local variable
  var hisDog = "Gromit"; // initializes local version of "hisDog"
  if (placeHolderElement) {
    newElem = document.createElement("div");
    newElem.className = "objProperty";
    newText = document.createTextNode("aBoy is: " + aBoy);
    // just picked up a global variable value
    newElem.appendChild(newText);
    placeHolderElement.appendChild(newElem);
    newElem = document.createElement("div");
    newElem.className = "objProperty";
    newText = document.createTextNode("His toyGlobal is " + toyGlobal);
    newElem.appendChild(newText);
    // just picked up another global variable value
    placeHolderElement.appendChild(newElem);
    // Do not bother with toyLocal here because it will throw undefined
    newElem = document.createElement("div");
    newElem.className = "objProperty";
    newText = document.createTextNode(
      "toyLocal value returned from the showLocal function is: " +
      showLocal());
    newElem.appendChild(newText);
    placeHolderElement.appendChild(newElem);
    newElem = document.createElement("div");
    newElem.className = "objProperty";
    newText = document.createTextNode("Local version of hisDog is: " + hisDog);
    newElem.appendChild(newText);
    // just picked up another global variable value
    placeHolderElement.appendChild(newElem);
    // now call another function that does not set the variable hisDog
    // and display the value. we’ll see that the global value is intact
    showGlobal();
  }
}
<script type="text/javascript" src="jsb-23-02.js"></script>
<h1>Variable scope</h1>
<div id="placeHolder"></div>

1 个答案:

答案 0 :(得分:2)

您的代码很可能是从类似the answer here之类的文件中复制的,该文件将addEvent定义为一种DRY方式来调用addEventListener(如果存在)或调用{{1} }(在Internet Explorer中使用的一种非常过时的方法,其作用与attachEvent相同):

addEventListener

与所有非内置函数一样,您需要定义function addEvent(element, eventName, fn) { if (element.addEventListener) element.addEventListener(eventName, fn, false); else if (element.attachEvent) element.attachEvent('on' + eventName, fn); } 函数才能对其进行调用。

addEvent在所有主要的浏览器(包括Internet Explorer 9 +)(2011年发布;如今只有少数人正在使用IE,而那些人几乎都在使用IE11)中都可以正常工作。我建议完全抛弃addEventListeneraddEvent,而仅使用attachEvent