如何将java脚本集成到html中?

时间:2016-07-23 22:42:09

标签: javascript html

我想知道我的代码如何显示在网页上而不是显示在警告框中,我该怎么做。我知道需要id,但我对从哪里开始有点困惑。任何帮助都会很好。三江源!



<!DOCTYPE html>
<html>

<script>



//Set of variables
var nameCheck = /^[a-zA-Z\s]+$/;
//eliminates anything not relevant
var numberCheck = /^[0-9\.]+$/;
//eliminates anything not relevant
var totHours = 0;
//adds total gaming hours on one day
var dayHours = 0;
//how many on one such day set in i from 1-7
var averHours = 0;
//stores the average by dividing by the tothours by 7
var mostPerDay = 0;
//calculates day with most gamed
var mostOnDay = 0;
//Most hours on ONE day
var moreDays = " ";
//adds an s to the end of days if more than one
var mpd = 0;
//most per day
var ah = 0;
//average hours
var th = 0;
//total hours
var name = prompt("What is your name?");
//asks users name


//Make sure user inputs a name that includes letters and or spaces
while (name == "null" || isNaN(name) == false || !name.match(nameCheck)){
	alert("Invalid Name!");
	name = prompt("What is your name?");
}

//Greets the user by name
alert("Hello " + name );

//Ask how many hours gamed on a day
for (var i = 1; i <= 7; i++){
	dayHours = prompt("How many hours have you gamed on day " + i + "?")

//Reask the question if the user inputs an invald answer
	while (dayHours == null || isNaN(dayHours) || dayHours > 24 || !dayHours.match(numberCheck) || dayHours < 0){
		alert("Incorrect! No letters or symbols, and make sure your input is under 24");
		dayHours = prompt("How many hours have you gamed on day " + i + "?")
	}

//Adds to total hours
totHours += Number(dayHours)
//Calculates days with most hours gamed
if (mostPerDay > dayHours){
}
else if (mostPerDay < dayHours){
	mostPerDay = Number(dayHours);
	mostOnDay = i;
}
else if (mostPerDay = dayHours){
	mostOnDay += " and " + i;
	mostPerDay = Number(dayHours);
}

}
//Adds 's' to the statistics if more than one day
if (isNaN(mostOnDay) == true){
	moreDays = "s ";
}
//Divides the total hours by 7 to get average over those 7 days
aver = (totHours / 7);
//Calculates and rounds to the value of 1
th = totHours.toFixed(1);
ah = aver.toFixed(2);
mpd = mostPerDay.toFixed(1);

//States calculated statistics
alert("\nTotal gaming hours this week " + th + "\nAverage gaming hours this week " + ah + "\nMost on one day" + moreDays + mostOnDay + " for " + mpd + " hours." );

//Comments on average hours per day gamed

if (averHours <= 2){
alert("Healthy amount of gaming this week")
}
else if (averHours <= 24){
	alert("Unhealthy amount of gaming this week")
}



</script>


</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

执行此操作的一种简单方法是包含指向外部javascript文件的链接:

<script src="path/myfile.js"></script>

位于html文件的底部。如果您的脚本需要jQuery,请确保它在脚本之前作为外部脚本链接。您可以通过为html标记提供id或类来引用javascript文件中的html元素。例如:

在HTML中:

<div id = "mydiv"> </div> 

在JS中选择元素:

$('#mydiv') 

答案 1 :(得分:0)

有几种方法可以在HTML文档中包含JavaScript:

  • 将JavaScript代码放在单独的filename.js文档中,并在HTML文档的标题中(即<head></head>之间)引用它,如下所示: <script type="text/javascript" src="filename.js"></script>。这是“最干净”的选项,因为它将功能(JavaScript)与结构(HTML)分开。

  • 将JavaScript代码直接放在HTML文档的标题中;也就是说,在<script type="text/javascript"></script>之间(此处没有src属性)

  • 在HTML文档的正文中,再次在<script></script>之间,例如,当您想要使用document.write('');

更改<div id="mydiv">中的文字可以通过id访问{<1}}来完成:

document.getElementById('mydiv').innerText = 'text';

或通过变体innerHTMLouterTextouterHTML

为了便于DOM操作,您可能需要查看jQuery。另外,请记住,标题或外部文件中的JavaScript代码将立即执行,如果尚未加载文档正文的某些部分,则可能会导致错误。 jQuery通过将代码包装在

中提供了一个优雅的解决方案
$(document).ready(function () {
    // code here
});
祝你好运!

答案 2 :(得分:0)

如果您试图让您的网页更具反应性,您可能需要查看jquery。它是一个轻量级的JavaScript库,可以帮助您使您的网页更具互动性。看看下面的教程:

http://www.w3schools.com/jquery/

我不完全理解你的问题,但是如果你问的是javascript是否真的会出现在你的网页上,除非你把它显示为文本,否则它不会出现。如果您想调试javascript代码,可以在Chrome上使用开发人员工具或其他类似的浏览器:

https://developer.chrome.com/devtools