jquer+vue自动提取生成目录

jquer+vue根据标题自动生成目录并且秒点定位,话不多说直接上代码。本

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>jquer+vue根据标题自动生成目录</title>
		<!-- 本地JS -->
		<script src="./jquery.js"></script>
			<!-- cdn方式VUE -->
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>//cdn VUE
		<style>
			#point{
				margin-bottom: 580px;
			}
			#point h1{
				font-size: 32px;
				color: red;
			}
			#point h2{
				font-size: 24px;
				color: aqua;
			}
			#point h3{
				font-size: 16px;
				color:#666;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<div>
				<p>锚点定位器</p>
				<div v-html="point" id="point"></div>
			</div>
			<div id="text">
				<h1>这里是一级标题A</h1>
				<h2>这里是二级标题</h2>
				<p>aaa</p>
				<h2>这里是二级标题</h2>
				<h2>这里是二级标题</h2>
				<h3>这里是三级标题</h3>
				<h1>这里是一级标题B</h1>
				<h2>这里是二级标题</h2>
				<h3>这里是三级标题</h3>
			</div>
		</div>
	</body>
	<script type="text/jscript">
		var vm = new Vue({
			el: "#app",
			data: {
				point: {}

			},
			mounted() {
				var points = $('#text') //内容列表
				var point = ''
				//循环获取内容的所有标签
				for (var i = 0; i < points[0].children.length; i++) {
					// 获取带H的标签
					if (points[0].children[i].localName.indexOf('h') !== -1) {

						// 获取h2
						if (points[0].children[i].localName.indexOf('h1') !== -1) {
							points[0].children[i].className = i

						}
						// // 获取h2
						if (points[0].children[i].localName.indexOf('h2') !== -1) {
							points[0].children[i].className = i
						}
						//获取h3
						if (points[0].children[i].localName.indexOf('h3') !== -1) {
							points[0].children[i].className = i
						}

						const title = $.extend(true, {}, points[0].children[i]);


						point += title.outerHTML
						points[0].children[i].id = `h1_id_${i}`
					}


				}

				this.point = point

				//循环获取标题标签给内容加点击事件

				//延迟0秒
				setTimeout(function() {

					//循环获取内容的所有标签
					for (var i = 0; i < $('#point')[0].children.length; i++) {
						// $('#point')[0].children[i].className 设置class名称

						//绑定点击事件根据className定位内容ID位置
						$($('#point')[0].children[i]).click(function(e) {
							location.href = `#h1_id_${e.target.className}`;
						})
					}
				}, 0); //延时10秒
			},
			methods: { //这个methods属性中定义了当前vue实例所有可用的方法

			}
		})
	</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SoberChi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值