我无法称呼外部JavaScript功能
我想学习JavaScript。因此,经过一些基础知识,我试图对Tictactoe进行编码。我写了HTML和CSS的东西,然后从JS开始。我在JS目录中创建了JS文件,并编写了第一个函数,即如果我单击DIV,则应在控制台中写入某些内容。然后我注意到该功能没有被调用。我不知道为什么。如果我在HTML文件中编写JS代码,则可以工作,如果我编写document.getElementById(“ id”)。onclick = function(){},但它与HTML中的OnClick选项不起作用。
# Html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TicTacToe</title>
<link type="text/css" rel="stylesheet" href="../css/Main.css">
<script type="text/javascript" rel="script" src="../js/TicTacToe.js"></script>
</head>
<body>
<div class="grid-container">
<label class="headline">X:0|0:Y</label>
<div id="item1" onclick="click()"></div>
<div id="item2" onclick="click()"></div>
<div id="item3" onclick="click()"></div>
<div id="item4" onclick="click()"></div>
<div id="item5" onclick="click()"></div>
<div id="item6" onclick="click()"></div>
<div id="item7" onclick="click()"></div>
<div id="item8" onclick="click()"></div>
<div id="item9" onclick="click()"></div>
</div>
</body>
</html>
#JavaScript File
//Initial constances
let player = true;
let xPlayerPoints = 0;
let yPlaxerPoints = 0;
function click(){
console.log("Test");
}
#CSS File
.headline{
grid-area: headspace;
background-color: #20c997;
border-radius: 5px;
font-size: 26px;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
}
.grid-container{
display: grid;
width: 33%;
margin-left: auto;
margin-right: auto;
grid-template-areas: 'headspace headspace headspace';
grid-template-columns: auto auto auto;
grid-gap: 5px;
padding: 5px;
background-color: #dde0e3;
border: 1px solid black;
border-radius: 5px;
}
.grid-container div{
cursor: pointer;
display: flex;
aspect-ratio: 1;
border-radius: 5px;
text-align: center;
background-color: White;
}
I want to learn JavaScript. So after some basics I was trying to code TicTacToe. I wrote the html and css stuff and then I started with js. I createt an Js file in the js directory and wrote the first function, an basic one, If I was clicking on a div, it should write something in the console. And then I noticed that the function was not called. I have NO IDEA WHY. It works if I write the Js code in the html file, it works if i write document.getElementByID("ID").onclick = function(){}, but it doesnt work with the onclick option in html.
# Html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TicTacToe</title>
<link type="text/css" rel="stylesheet" href="../css/Main.css">
<script type="text/javascript" rel="script" src="../js/TicTacToe.js"></script>
</head>
<body>
<div class="grid-container">
<label class="headline">X:0|0:Y</label>
<div id="item1" onclick="click()"></div>
<div id="item2" onclick="click()"></div>
<div id="item3" onclick="click()"></div>
<div id="item4" onclick="click()"></div>
<div id="item5" onclick="click()"></div>
<div id="item6" onclick="click()"></div>
<div id="item7" onclick="click()"></div>
<div id="item8" onclick="click()"></div>
<div id="item9" onclick="click()"></div>
</div>
</body>
</html>
#JavaScript File
//Initial constances
let player = true;
let xPlayerPoints = 0;
let yPlaxerPoints = 0;
function click(){
console.log("Test");
}
#CSS File
.headline{
grid-area: headspace;
background-color: #20c997;
border-radius: 5px;
font-size: 26px;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
}
.grid-container{
display: grid;
width: 33%;
margin-left: auto;
margin-right: auto;
grid-template-areas: 'headspace headspace headspace';
grid-template-columns: auto auto auto;
grid-gap: 5px;
padding: 5px;
background-color: #dde0e3;
border: 1px solid black;
border-radius: 5px;
}
.grid-container div{
cursor: pointer;
display: flex;
aspect-ratio: 1;
border-radius: 5px;
text-align: center;
background-color: White;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有两个问题。
的
而是使用Say
handleclick
。完整工作 codepen 检查控制台是否输出。There are two issues.
click
, instead use sayhandleClick
.Full working codepen Check the console for log output.
通过DOM API连接事件处理程序,即。使用
.addeventListener
。尽管如此建议,但无需使用此解决方案重命名处理程序功能。
在下面的实时演示中,CSS A和JS编程被定义为内联,但是通过链接包含这些零件是完全可以的。
有关更多信息,请参见 docs(mdn)。
Attach the event handler through the DOM API, ie. using
.addEventListener
.No need to rename the handler function with this solution, though it is recommended nonetheless.
In the live demo below, CSS an JS programming are defined inline, but it is perfectly fine to include these parts through links.
For more info see the docs (MDN).
这是因为您仅定义一个函数而不将其调用代码中的任何位置,
还可以确保在HTML中使用JavaScript文件。了解更多。 https://wwwwwww.w3schools.com/tags/tags/tags/tags/att_script_src.src_src.asp
javascript在这里: javascript函数
It is because you are only defining a function and not calling it anywhere in the code
Also make sure use Add a javascript file to your html. Learn more. https://www.w3schools.com/tags/att_script_src.asp
Learn more about Javascript here : Javascript function
您需要使用它的所有元素在所有元素下方和身体端标签上方包含脚本标签,否则您可以执行此操作。
在脚本标签中包括延期,它将像
在使用脚本标签时一样起作用,它的作用就像JS的正常情况一样,并且按照数值顺序执行所有操作,因此它正在执行的是执行JS文件没有HTML的任何信息。希望这有所帮助
You need to include the script tag below all elements using it and above the body end tag or you could do this.
Include defer in the script tag and it will act as if it were at the bottom
When you use the script tag it acts just as js normally does and it executes everything by line in numerical order so what it is doing is its executing the js file without any information from the html. Hope this helped