![](/img/trans.png)
[英]Uncaught ReferenceError: function is not defined with onclick
[英]Uncaught ReferenceError: is not defined onclick
在onclick上,我遇到了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
描述的元素#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”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.