赞
踩
- <!DOCTYPE html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script>
- var codeTxt = '(function() { console.log(document.querySelector(".title").innerHTML) })()';
- function run() {
- console.log('执行js字符串');
- // 第一种方法
- // let func = new Function(this.codeTxt);
- // func();
-
- // 第二种方法
- // eval(codeTxt)
-
- // 第三种
- setTimeout(codeTxt)
- }
-
- // html元素发生变化:
- function htmlChange() {
- console.log("html发生了变化");
- }
- </script>
- </head>
-
- <body onchange="htmlChange">
- <h3 class="title">执行jsStr:</h3>
- <button onclick="run()">开始执行</button>
- </body>
-
- </html>

运行js字符串代码的三种方法
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。