简体   繁体   English

如何在我的 HTML 中运行 JavaScript function

[英]How can I run JavaScript function in my HTML

I wrote a function in JS code, and I want to run it from HTML, but I don't see any reaction, when I run the site.我在 JS 代码中写了一个 function,我想从 HTML 运行它,但是当我运行该站点时,我没有看到任何反应。

I will show you example of html code and js code.我将向您展示 html 代码和 js 代码的示例。

<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="test.js">
    <script src="test.js"></script>
</head>

<body onload="add()"> 
    
    <p id="add2"></p>
</body>

</html>

Here starts JS code这里开始JS代码

function add(a,b,c,d) {
  return a + b + c + d;
}

document.getElementById("add2").innerHTML = add(5,10,15,20); 

I hope I wrote it clearly and someone will tell me, what did I do wrong?我希望我写得很清楚,有人会告诉我,我做错了什么?

Your JavaScript code is in the head , which is before the body and everything else, so the JavaScript runs before the p element has been created (html is run line by line).您的 JavaScript 代码位于head中,位于body和其他所有内容之前,因此 JavaScript 在创建p元素之前运行(html 逐行运行)。 To fix this issue, you can try putting the JavaScript at the end of the document, for example after body .要解决此问题,您可以尝试将 JavaScript 放在文档末尾,例如在body之后。

When a browser loads an HTML page, it reads your HTML from top-to-bottom.当浏览器加载 HTML 页面时,它会从上到下读取您的 HTML。

So ordering is important.所以排序很重要。

There are complexities of course.当然也有复杂性。 I'm not talking about deferred or asynchronous scripts here.我在这里不是在谈论延迟或异步脚本。 But the top-to-bottom simplification helps us understand your problem.但是从上到下的简化有助于我们了解您的问题。

Your script is inside test.js , so it will be loaded and run before [body] is ready.您的脚本位于test.js中,因此它将在 [body] 准备好之前加载并运行。

But test.js has this line:但是test.js有这一行:

document.getElementById("add2").innerHTML = add(5,10,15,20); 

This line is not inside a function, so the browser will try to run it immediately.此行不在 function 内,因此浏览器将尝试立即运行它。

The call to add() will work because it has been declared in the file.add()的调用将起作用,因为它已在文件中声明。 But document.getElementById("add2") will not, because it is an instruction to access the following HTML in the [body]:但是document.getElementById("add2")不会,因为它是访问[body]中以下HTML的指令:

<p id="add2"></p>

But the [body] has not been "read" yet, so the JavaScript DOM API does not know about it.但是 [body] 还没有被“读取”,所以 JavaScript DOM API 不知道它。

However, you have partially solved the problem already using the onload attribute of the [body] tag.但是,您已经使用 [body] 标记的onload属性部分解决了问题。 You've just used the wrong function with it.您刚刚使用了错误的 function 。

That onload is an instruction to run a function after [body] has been completely read.onload是在 [body] 被完全读取运行 function 的指令。 So if you changed your document.getElementById line to be inside a function:因此,如果您将document.getElementById行更改为 function 内:

function runWhenBodyHasLoaded () {
  document.getElementById("add2").innerHTML = add(5,10,15,20); 
}

And told the [body] tag to run the function after everything has loaded:并告诉 [body] 标签在所有内容加载后运行 function:

<body onload="runWhenBodyHasLoaded()">

Then <p id="add2"></p> will be ready in time to access it with document.getElementById .然后<p id="add2"></p>将及时准备好使用document.getElementById访问它。

Or with jQuery: https://jsfiddle.net/bdgu8s4h/1/或使用 jQuery: https://jsfiddle.net/bdgu8s4h/1/

Also consider loading your JS at the end (of body)还可以考虑在最后(正文)加载您的 JS

<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="test.css">
</head>

<body> 
    <p id="add2"></p>
    <script type="text/javascript" src="test.js"></script>
</body>

</html>

JS (jQuery): JS(jQuery):

$(document).ready(function() {
function add(a,b,c,d) {
    return a + b + c + d;
}
    $('#add2').html(add(5,10,15,20));
});

Hope this also helped, cheers希望这也有帮助,干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript - 如何在所有 html 行中运行 javascript - JavaScript - How can I run the javascript in all my html rows 我如何从 HTML 文件中调用我的 Javascript function - How I can call my Javascript function from HTML file 如何在Javascript global中创建一个函数到我的html - How can I make a function in Javascript global to my html 如何运行在24/7的html中找到的javascript脚本? - How can I run my javascript scripts found in my html 24/7? 如何从我的php文件中调用位于我的html文件中的javascript函数? - How can I call a javascript function located in my html file from my php file? 每次调整 HTML 元素的大小时,如何运行 JavaScript 函数? - How can I run a JavaScript function every time an HTML element is resized? 如何通过输入字段和按钮作为 HTML 中的事件运行 if/else Javascript function? - How can I run an if/else Javascript function through and input field and a button as the event in HTML? 如何在我网站的所有不同页面上运行JavaScript函数? - How can I run a JavaScript function across all of my website different pages? 如何最小化我的JavaScript函数? - how can i minimize my javascript function? 我如何使用javascript函数(例如html表单的“ post”方法)的结果? - how can i use the result from a javascript function like the 'post' method of my html forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM