未捕获的ReferenceError:未在单击时定义

Uncaught ReferenceError: is not defined onclick

本文关键字:单击 定义 ReferenceError      更新时间:2023-09-26

点击后,我将获得Uncaught ReferenceError: akkiTooltip is not defined
检查我正在使用的代码:

$(document).ready(function () {
    function akkiTooltip() {
        function applyTooltip(element, word) {
            var tooltipText = $('' + element + ' label').data('tooltip');
            var newContent = $('' + element + ' label').html().replace(word, '<span data-tooltip="' + tooltipText + '">' + word + '</span>');
            $('' + element + ' label').removeAttr('data-tooltip');
            return $('' + element + ' label').html(newContent);
        }
        applyTooltip('#question_307', 'Some text');
    }
});

这是来自我正在应用onclick:的另一个js文件

$(button)
    .text(this.answers[a].title)
    .attr('type', 'button')
    .attr('class', 'btn')
    .attr('onclick', 'quiz._makeSelection(''answer_' + this.answers[a].id + ''', ' + go + ', ' + this.answers[a].skipToSection + '); akkiTooltip();');

在将akkiTooltip功能移动到$(document).ready功能之外后,我得到了这个错误

Uncaught TypeError: Cannot read property 'replace' of undefined

akkitooltip函数的定义移动到document.ready函数之外。从那里无法进入。

Cannot read property 'replace' of undefined

查看您在哪里使用.replace

$('' + element + ' label').html().replace(word, '...');

因此,$('' + element + ' label').html()不是您所期望的,即$('' + element + ' label')没有找到元素,因此.html()没有返回字符串

检查选择器#question_307 label描述的元素是否存在


当我尝试重现这个错误时,我实际上得到了null而不是undefined,所以它可能是您的代码的不同部分

也许您应该检查您是否设置正确:

1.在html和jsp网页中:

<script src=""></script>

2.在xhtml网页中:

<script src="" />

3.如果你的js代码(函数代码)和html在同一个网页上,你可以使用:

<script> {your js code} </script>

默认脚本类型为"test/javascript"