XIYUETA.COM
源代码:
点击运行
<script src="https://www.xiyueta.com/js/xiyueta.min.js?v36"></script> <script> /* $.load() 加载解析HTML .each() 遍历匹配元素,为每个匹配元素规定运行的函数,返回false为停止循环 .text() 获取匹配元素集合中每个元素的组合文本内容(包括它们的后代),或设置匹配元素的文本内容 .html() 获取匹配元素集中第一个元素的HTML内容,或设置每个匹配元素的HTML内容 $.html 打印HTML对象全部内容,与cheerio库里使用一致 $.log() 在浏览器里显示内容 == document.write() */ var html = [ '<style>', ' p {', ' margin: 8px;', ' font-size: 20px;', ' color: blue;', ' cursor: pointer;', ' }', ' b {', ' text-decoration: underline;', ' }', ' button {', ' cursor: pointer;', ' }', ' </style> ', ' ', '<p>', ' <b>Click</b> to change the <span id="tag">html</span>', '</p>', '<p>', ' to a <span id="text">text</span> node.', '</p>', '<p>', ' This <button name="nada">button</button> does nothing.', '</p>' ].join("\n"); $.load(html) document.write($.html()) $("p").each(function() { var htmlString = $(this).html(); $(this).text(htmlString); }); $.log($.html()) document.write($.html()) </script>
运行结果