首页> 基础笔记 >JS/JQ基础学习 >基础语法 基础语法

在HTML中使用JavaScript

作者:小萝卜 2023-03-20 浏览 236

简介1.使用<script>JS代码</script>标签2.使用<script></script>标签导入js文件3.1 使用事件传入JavaScript代码

1.使用<script>JS代码</script>标签      

src属性:表示包含要执行代码的外部文件。      

type属性:text/javascript(可省略)      

<script type=“text/javascript”>    


 alert(‘hello,js’);      


</script>

 

2.使用<script></script>标签导入js文件。

注意:当使用script标签的src属性时,script标签中一定不能放入任何JS代码。

例如:     
 

<script src=‘test.js’>         之间不允许加入任何JS代码….     </script>


注意:<script>标签可以放到html页面的任何地方。

3.1 使用事件传入JavaScript代码 <div onclick=“alert(‘hello,js’)”></div>

3.2 使用<a href=“javascript:alert(‘hello,js’);”></a>

练习:
 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>javascript的使用场景</title>
</head>
<body>
	<!-- 第三种  事件属性 -->
	<div id="love" style="width:200px;height:200px;background:lightgreen"  onclick="alert('你点我干啥??')"></div>
	<!-- 第四种  a标签 -->
	<a href="javascript: alert('你跳到百度干啥???');" >百度</a>
	
	<script type="text/javascript">
	//第一种 <!-- script标签 -->
		// alert("今天貌似天气不错...");
	</script>

	<!-- 第二种 -->
	<script src="./2.js"></script>
</body>
</html>
 

 

很赞哦! (0)

文章评论

    高端网站建设