原型JS:文档.观察火灾&;undefined不是一个函数&;错误

Prototype JS: Document.observe fires "undefined is not a function" error

本文关键字:错误 函数 一个 undefined JS 文档 观察 原型 火灾      更新时间:2023-09-26

我尝试使用事件。方法,由Prototype JS提供。为了确保DOM被加载,我使用了document.observe。这会导致错误Uncaught TypeError: undefined不是函数。原型JS被加载,如下所示:

<!DOCTYPE html>
<html>
<head>
    <title>playground</title>
    <script language="javascript" type="text/javascript" src="../js/prototype.js"></script>
    <script language="javascript" type="text/javascript" src="../js/functions.js"></script>
</head>
<body>
    <div>
        <a href="#" id="meinLink">Hello World</a>
    </div>
    <input type="button" onclick="changeLinkText()" value="Change link by using Prototype">
</body>

JavaScript

//this works
var changeLinkText = function(){
    $("meinLink").innerHTML="prototypeWorks";
};
//this causes the error
Document.observe("dom:loaded", function() {
    $("meinLink").observe('click', function(e) {
        document.getElementById('meinLink').innerHTML="prototypeDoesntWork";
    });
});

您的D

Document.observe("dom:loaded", function() {

是大写的,将其固定为正确的符号

document.observe("dom:loaded", function() {

可能会做这件事。

您也可以尝试调用on-click-event侦听器。

$$('meinLink').invoke('on', 'click', '.item', function(event, el) {
    // el is the 'meinLink' element
});

甚至有可能只使用on就能完成这项工作。

$("meinLink").on("click", function(event) {
   document.getElementById('meinLink').innerHTML="prototypeDoesntWork";
});