如何使用JS取出<script>标签中的文本

如题所述

首先,只能取出inline的JS内容,如果脚本是通过 src 加载进来的,这个是没办法取出的。

对于 inline 的 JS 内容,高端浏览器使用 textContent ,IE6/7/8 使用 innerText。下面是例子:

<script id="s1">alert(1);</script>

var script = document.getElementById( 's1' );

var text = script.textContent || script.innerText;

console.log( text ); // output alert(1);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-12-19
首先,只能取出inline的JS内容,如果脚本是通过 src 加载进来的,这个是没办法取出的。对于 inline 的 JS 内容,高端浏览器使用 textContent ,IE6/7/8 使用 innerText。下面是例子:var script = document.getElementById( 's1' );var text = script.textContent || script.innerText;console.log( text ); // output alert(1);

搜索
javascript编程入门
js必学的十个技能
前端获取数据的方式
爬虫的正确编程方法
脚本模板大全
javascript语法讲解
相似回答