因此,我倾向于先在html文件中创建一个脚本,然后在我知道它可以工作时将其移动到javascript文件中。但是,现在它返回的是未定义的文件。
这是我的html:
<div class="tab">
<button class="tablinks" onclick="openAction(event, 'battle-wrapper')" id="defaultOpen">Battle</button>
<button class="tablinks" onclick="openAction(event, 'tradeskill-wrapper')">Trade Skills</button>
<button class="tablinks" onclick="openAction(event, 'crafting-wrapper')">Crafting</button>
</div>
这是我的JavaScript:
var storedUsername;
// Port Connect Cht
if (window.location.hostname == '****' || window.location.hostname == '****') {
var port = 443;
} else {
var port = 8080;
}
var connected = false;
var socket = io.connect(window.location.hostname + ':' + port, { 'connect timeout': 5000 });
// Connection Successful
socket.on('connect', function () {
connected = true;
// socket.emit('login');
});
socket.on('disconnect', function () {
connected = false;
});
$(document).on('click', 'button#btn-logout', function () {
socket.emit('logout');
});
socket.on('not logged', function (destination) {
window.location.href = destination;
});
socket.on('start up', function (dataObj) {
});
$(document).ready(function (dataObj) {
$('#battle-wrapper').on('click', 'button#battlebutton', function (event) {
event.preventDefault();
socket.emit('fight mob', $('#monster').val());
});
socket.on('mob test', function (dataObj) {
if (dataObj.userStats) {
$('#player-stats').html(dataObj.userStats);
$('#mob-stats').html(dataObj.mobStats);
$('#fight-results').html(dataObj.results);
$('span#action-counter').html(dataObj.autos);
$('#title').html(dataObj.autos + ' - KoG');
} else {
$('#title').html('AUTOS EXPIRED - KoG');
}
});
$('#generalChat').click(function () {
$('#chat').toggle();
});
var current = '#actions';
// Actions Button Handler
$('#btn-actions').click(function () {
if (current != '#actions') {
$(current).hide();
current = '#actions';
$(current).show();
}
});
// Profile button handler
$('#btn-profile').click(function () {
if (current != '#profile') {
$(current).hide();
current = '#profile';
$(current).show();
}
});
// Kingom button handler
$('#btn-kingdom').click(function () {
if (current != '#kingodm-page') {
$(current).hide();
current = '#kingdom-page';
$(current).show();
}
});
// inventory button handler
$('#btn-inventory').click(function () {
if (current != '#inventory') {
$(current).hide();
current = '#inventory';
$(current).show();
}
});
// allicne button handler
$('#btn-alliance').click(function () {
if (current != '#alliance') {
$(current).hide();
current = '#alliance';
$(current).show();
}
});
// mail button handler
$('#btn-mail').click(function () {
if (current != '#mail') {
$(current).hide();
current = '#mail';
$(current).show();
}
});
// Statistics Handler
$('#statistic-row').load('statistics.ejs', function () {
socket.on('time', function (data) {
$('#time').html(data);
});
socket.on('users online', function (usersConnected) {
$('#online').html(usersConnected);
});
});
});
// Chat Handler
$('#chat').load('chat.ejs', function () {
$('#chat-form').submit(function (event) {
event.preventDefault();
var msg = $('#m').val();
// Clears chat field
$('#m').val('');
// Discards message if not connected
if (!connected || msg == '') {
return false;
}
socket.emit('chat', msg, storedUsername);
});
// Populat chat
socket.on('message', function (message, username) {
storedUsername = username || storedUsername;
$('#messages').prepend($('<li>').html(message));
});
// Get the element with id="defaultOpen" and click on it
document.getElementById('defaultOpen').click();
function openAction(evt, actionName) {
// Declare all variables
var i; var tabcontent; var tablinks;
console.log(actionName);
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName('tabcontent');
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = 'none';
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName('tablinks');
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(' active', '');
}
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(actionName).style.display = 'block';
evt.currentTarget.className += ' active';
}
});
**相关功能已结束 现在,我知道脚本本身在html文件中有效。我正确地包含了我的JS文件,因为它使用了许多其他功能,并且它们可以工作。但是由于某种原因,该功能是唯一无法正常工作的。
编辑:这是完整的控制台错误:
Uncaught ReferenceError: openAction is not defined
at HTMLButtonElement.onclick (game:66)
at HTMLDivElement.<anonymous> (game.js:152)
at HTMLDivElement.<anonymous> (jquery.min.js:4)
at Function.each (jquery.min.js:2)
at r.fn.init.each (jquery.min.js:2)
at Object.<anonymous> (jquery.min.js:4)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at A (jquery.min.js:4)
at XMLHttpRequest.<anonymous> (jquery.min.js:4)
编辑:我添加了整个JS文件,也许你们中的一个可以发现我无法识别的错误。我还向要检查的内容中添加了****
。
答案 0 :(得分:2)
问题是范围之一。您已经在以openAction()
开头的另一个函数中定义了函数$('#chat').load('chat.ejs', function () { ...
,HTML无法访问该上下文。
您的HTML期望它是全局的,因此将openAction()
的定义移到当前包含它的函数下。
答案 1 :(得分:0)
提供的信息我理解正确:如果当它们都属于一个文件时一切正常,但是将脚本重定位到单独的文件时得到的功能未定义,则可能是排序/排序问题之一。
您可以在函数调用之前加载外部脚本(之所以起作用,是因为脚本被解析器阻止,并且它将在紧随其后的脚本块之前评估文件)
<div class="tab">
<button class="tablinks" onclick="openAction(event, 'battle-wrapper')" id="defaultOpen">Battle</button>
<button class="tablinks" onclick="openAction(event, 'tradeskill-wrapper')">Trade Skills</button>
<button class="tablinks" onclick="openAction(event, 'crafting-wrapper')">Crafting</button>
</div>
<script src='some_external_script_containing_openAction.js'></script>
<script>
// Get the element with id="defaultOpen" and click on it
document.getElementById('defaultOpen').click();
</script>
或者您可以将触发代码放置在onload处理程序中(个人喜好)...
<div class="tab">
<button class="tablinks" onclick="openAction(event, 'battle-wrapper')" id="defaultOpen">Battle</button>
<button class="tablinks" onclick="openAction(event, 'tradeskill-wrapper')">Trade Skills</button>
<button class="tablinks" onclick="openAction(event, 'crafting-wrapper')">Crafting</button>
</div>
<script>
//wait for all resources to load.
window.addEventListener('load', function(e){
// Get the element with id="defaultOpen" and click on it
document.getElementById('defaultOpen').click();
});
</script>
<script src='some_external_script_containing_openAction.js'></script>
但是我没有足够的信息可以肯定地诊断您的问题。如果这些解决方案都不适合您的情况,请在脚本文件中放入console.log('here')
,并确保它在页面中被触发(F12通常会打开控制台)。如果不是这样的话,您可能对链接到文件的方式有疑问。