前端基础应用:js文件编写

在这里插入图片描述
jss文件在前端网页页面制作过程中作用主要是控制html文件中标签的逻辑问题,增强操作性实用性.

一、js的作用,写代码的位置

1)作用

1.修改双标签的标签内容

例如:改变他的文本

<p id="p1">我是段落1</p>
<button onclick="document.getElementById('p1').innerText = 'hello world!'">修改</button>

2.修改标签属性

例如:改变他的网址链接

<a id="a1" href="https://www.baidu.com" target="_blank">打开网页</a>
<button onclick="document.getElementById('a1').href = 'https://www.taobao.com/'">淘宝</button>

3.修改标签样式

例如:改变段落的颜色和随时间改变它的字体大小

<p id="p3">我是段落2</p>
<script>
function changeColor(){
     
		// 0 ~ 1
		r = parseInt(Math.random() * 255)
		g = parseInt(Math.random() * 255)
		b = parseInt(Math.random() * 255)
		document.getElementById('p3').style.color = 'rgb('+r+', '+g+', '+b+')'
	}
	
	fs = 10
	
	t1 = setInterval(function(){
     
		r = parseInt(Math.random() * 255)
		g = parseInt(Math.random() * 255)
		b = parseInt(Math.random() * 255)
		document.getElementById('p3').style.color = 'rgb('+r+', '+g+', '+b+')'
		
		fs += 2
		if(fs >= 40){
     
			fs = 10
		}
		document.getElementById('p3').style.fontSize = fs + 'px'
	}, 500)

</script>

在这里插入图片描述

2)代码位置

.js代码写在哪儿?
1)将js代码写在标签的事件属性中

<button onclick="alert('你好!')">确定</button>

2)将js代码写在script标签中(script标签可以放在html文件中的任何位置)

<body>
<p id="p1">我是段落1</p>
</body>
<script>
	document.getElementById('p1').innerText = 'hello world!'
</script>

3)将js代码写在外部的js文件中,然后在html文件中用script标签导入
可以直接编写js文件,用下列代码导入html文件

<script src="js/demo.js"></script>

二、DOM操作标签

DOM(document object mode)操作:
通过document对象直接或者间接的操作网页内容
js中的document对象代表网页内容

DOM操作主要内容:
获取标签、添加标签、删除标签、操作标签内容、操作标签属性、操作标签样式

1.获取标签

1)直接获取标签 - 直接通过document对象获取标签

document.getElementById(id属性值)

获取网页中id属性值为指定值的标签,返回值是标签对象

document.getElementsByClassName(class属性值)

获取网页中class属性值为指定值的所有标签,返回值是数组(列表),数组中的元素是标签对象

document.getElementsByTagName(标签名)

获取网页中所有指定的标签,返回值是数组(列表),数组中的元素是标签对象

document.getElementsByName(name属性值)

获取网页中所有name属性值为指定值的标签

2)间接获取标签 - 通过父标签获取子标签,或者通过子标签获取父标签
a. 获取一个标签所有的子标签: 标签对象.children
b.获取一个标签的第一个子标签: 标签对象.firstElementChild
c.获取最后一个子标签:标签对象.lastElementChild
d.获取父标签: 标签对象.parentElement

*console.log(result)*在网页控制台中获取result的值

<body>
	
	<div id="box1">
		<p class="c1">我是段落1</p>
		<a>我是超链接1</a>
		<span class="c1">我是span1</span>
		<p id="p1">我是段落2</p>
		<p>我是段落3</p>
	</div>
	
	<div id="box2">
		<span>我是span2</span>
		<a class="c1">我是超链接2</a>
		
		<input type="text" name="username" id="">
		<input type="radio" name="gender"><input type="radio" name="gender"></div>
	
	
</body>

<!-------------- 1)直接获取标签 -------------->
<script type="text/javascript">
	// 通过id值获取标签
	result = document.getElementById('p1')
	console.log(result)
	
	// 通过class属性获取标签
	result = document.getElementsByClassName('c1')
	console.log(result)
	console.log(result[2])
	
	// 通过标签名获取标签
	result = document.getElementsByTagName('p')
	console.log(result)
	
	// 通过name属性值获取标签(了解)
	result = document.getElementsByName('gender')
	console.log(result)
</script>

<!-------------- 2)获取子标签和父标签 -------------->
<script>
	console.log('----------------------------------------------')
	// 1.获取子标签
	var box1 = document.getElementById('box1')
	// 1)获取一个标签所有的子标签: 标签对象.children
	result = box1.children
	console.log(result)
	
	// 2)获取一个标签的第一个子标签
	result = box1.firstElementChild
	console.log(result)
	
	// 3)获取最后一个子标签
	result = box1.lastElementChild
	console.log(result)
	
	// 2.获取父标签
	result = box1.parentElement
	console.log(result)
	
	var p = document.getElementById('p1')
	result = p.parentElement
	console.log(result)
</script>

在这里插入图片描述

三、添加和删除标签

1.添加标签: 创建标签 -> 添加标签

1)创建标签:document.createElement(标签名)

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
你可以通过两种方式引入Vue脚手架。一种是将Vue作为一个模块进行引入,这需要使用构建工具(如webpack)来加载模块,并在源码中使用require('vue')来引入Vue模块。另一种方式是直接在HTML文件中使用<script>标签引入vue.js文件,这样就可以直接使用全局的Vue变量,而不需要构建工具。 在前端JS文件中引入Vue脚手架可以通过以下两种方式实现: 1. 使用模块化的方式引入Vue:通过安装Vue的包管理器,如npm,在你的前端JS文件中使用require或import语句来引入Vue模块。例如: ``` const Vue = require('vue'); // 或者 import Vue from 'vue'; ``` 2. 直接在HTML文件中引入Vue:在你的HTML文件中使用<script>标签来引入vue.js文件。例如: ``` <script src="path/to/vue.js"></script> ``` 以上两种方式都可以让你在前端JS文件中使用Vue脚手架来编写代码。选择哪种方式取决于你的项目需求和个人偏好。如果你需要更复杂的工程化管理和依赖管理,建议使用模块化的方式引入Vue脚手架。如果你只是需要快速搭建一个简单的页面或应用,可以直接在HTML文件中引入Vue脚手架。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [前端引入vue.js和使用vue脚手架的区别?](https://blog.csdn.net/weixin_44226391/article/details/125742696)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喆子玩点数据

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值