XIYUETA.COM
源代码:
点击运行
<script src="https://www.xiyueta.com/js/xiyueta.min.js?v36"></script> <script> /* $.load() 加载解析HTML .find() 获取当前匹配元素集合中每个元素的后代 .odd() 将匹配元素的集合减少到集合中的奇数元素,从零开始编号 .addClass() 将指定的类添加到匹配元素集中的每个元素 $.html 打印HTML对象全部内容,与cheerio库里使用一致 $.log() 在浏览器里显示内容 == document.write() */ var html = [ ' <style>', ' p {', ' font-size: 20px;', ' width: 200px;', ' color: blue;', ' font-weight: bold;', ' margin: 0 10px;', ' }', ' .hilite {', ' background: yellow;', ' }', ' </style>', '<p>', ' When the day is short', ' find that which matters to you', ' or stop believing', '</p>' ].join("\n"); xiyueta.load(html); var newText = $("p").text().split(" ").join("</span> <span>"); newText = "<span>" + newText + "</span>"; $("p").html(newText); xiyueta.load(xiyueta.html()); //重新解析html $("p").find("span").odd().addClass("hilite"); $.log(xiyueta.html()) document.write(xiyueta.html()) </script>
运行结果