使用jQuery为博客生成目录
2014-09-09 16:37
260 查看
这段代码展示了如何为div#content中的内容生成目录,也无非是对h系列标记进行解析。当然,也早有一些人实现了。
1. [代码][HTML]代码
<html>
<head>
<title> 测试 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="../lib/js/jquery-1.7.1.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
//index01
$("#index01").text($("#content").html());
//index02
x = $("h1,h2").toArray();
c = "";
for (i = 0; i < x.length; i++)
{
c = c + x[i].innerHTML + "<br />";
}
$("#index02").html(x + "<br/>" + c);
//index03
x = $("h1,h2");
c = "";
x.each(function() {
c = c + $(this).text() + " " + $(this).prop('tagName') + "<br/>"; //prop()与attr()的区别
});http://www.huiyi8.com/hunsha/lifu/
$("#index03").html(c);
//index04
//generate anchor
x = $("h1,h2");
n = 0;
c = "";
x.each(function() {
$(this).before("<a name='anchor" + n + "'></a>");
if ($(this).prop('tagName') == "H1") //不可用"h1"
{婚纱礼服欣赏
c=c+"<a href='#anchor"+n+"' class='anchor-h1'>"+$(this).text()+"</a><br/>";
}
else {
c=c+"<a href='#anchor"+n+"' class='anchor-h2'>"+$(this).text()+"</a><br/>";
}
n = n + 1;
});
$("#index04").html(c);
});
</script>
<div id="index01"></div><hr/>
<div id="index02"></div><hr/>
<div id="index03"></div><hr/>
<div id="index04"></div><hr/>
<div id="content">
<h1>第一章</h1>
<h2>第1节</h2>
<p>你好</p>
<h2>第2节</h2>
<h1>第二章</h1>
<h2>第1节</h2>
<h2>第2节</h2>
</div>
</body>
</html>
2. [图片] 01.jpg
1. [代码][HTML]代码
<html>
<head>
<title> 测试 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="../lib/js/jquery-1.7.1.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
//index01
$("#index01").text($("#content").html());
//index02
x = $("h1,h2").toArray();
c = "";
for (i = 0; i < x.length; i++)
{
c = c + x[i].innerHTML + "<br />";
}
$("#index02").html(x + "<br/>" + c);
//index03
x = $("h1,h2");
c = "";
x.each(function() {
c = c + $(this).text() + " " + $(this).prop('tagName') + "<br/>"; //prop()与attr()的区别
});http://www.huiyi8.com/hunsha/lifu/
$("#index03").html(c);
//index04
//generate anchor
x = $("h1,h2");
n = 0;
c = "";
x.each(function() {
$(this).before("<a name='anchor" + n + "'></a>");
if ($(this).prop('tagName') == "H1") //不可用"h1"
{婚纱礼服欣赏
c=c+"<a href='#anchor"+n+"' class='anchor-h1'>"+$(this).text()+"</a><br/>";
}
else {
c=c+"<a href='#anchor"+n+"' class='anchor-h2'>"+$(this).text()+"</a><br/>";
}
n = n + 1;
});
$("#index04").html(c);
});
</script>
<div id="index01"></div><hr/>
<div id="index02"></div><hr/>
<div id="index03"></div><hr/>
<div id="index04"></div><hr/>
<div id="content">
<h1>第一章</h1>
<h2>第1节</h2>
<p>你好</p>
<h2>第2节</h2>
<h1>第二章</h1>
<h2>第1节</h2>
<h2>第2节</h2>
</div>
</body>
</html>
2. [图片] 01.jpg
相关文章推荐
- 使用Jquery+repeater生成可以收缩的目录
- 使用Python生成博客目录并自动更新README
- 如何在 Word 中使用自定义样式生成文章目录
- 如何在使用pdfFactory Pro生成PDF时自动创建目录书签
- ASP.NET中使用后端代码注册脚本 生成JQUERY-EASYUI的界面错位的解决方法
- [jQuery]使用jQuery.Validate进行客户端验证——不使用微软验证控件的理由-目录及简介
- [jQuery]使用jQuery.Validate进行客户端验证——不使用微软验证控件的理由-目录及简介
- 使用函数自动生成n层目录
- 使用jquery动态生成的标签,需要在代码当中绑定才可以实现事件的监听
- ASP.NET中使用后端代码注册脚本 生成JQUERY-EASYUI的界面错位的解决方法
- 图片上传生成缩略图,并使用JQuery实现鼠标移动到缩略图显示相应的大图的例子
- 使用md5sum递归生成整个目录的sum
- ASP.NET2.0 TreeView控件使用数据库生成目录
- Axure RP Pro - jQuery - 如何在Axure RP Pro生成的HTML原型文档中使用自定义脚本
- 使用dos命令生成目录树
- [原]ASP.NET中使用后端代码注册脚本 生成JQUERY-EASYUI 的界面错位
- 配合Ajaxpro读取无限父子关系部门使用javascript生成的树形目录
- 在vs.net2008中使用CuteEditor编辑器时,当发布网站时,生成的CuteSoft_Client\CuteEditor 目录中会缺少ditor.cep文件。
- php+jquery读取文件目录生成树形结构
- 使用Quartz 定时生成话单、把话单上传FTP、把话单移动到备份目录