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

2025-03-22 05:09:50
推荐回答(2个)
回答1:

首先,只能取出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);

回答2:

首先,只能取出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语法讲解