js不能对script节点操作?
<script language= "javascript " type= "text/javascript " >
var i=document.getElementsByTagName( "script ");
i[1].setAttribute( "src ", "2.js ");
window.alert( "ok ");
</script>
<script language= "javascript " src= "1.js ">
</script>
我想把第二个script的src属性去掉,为什么不可以?
只用getElementsByTagName()方法。
------解决方案--------------------
<script language= "javascript " src= "1.js ">
</script>
<script language= "javascript " type= "text/javascript " >
var i=document.getElementsByTagName( "script ");
i[0].setAttribute( "src ", "2.js ");
window.alert( "script 1 'src: "+i[0].src+ "ok ");
</script>
------解决方案--------------------
还真没这么用过,个人认为文档解释完,即使移除src属性,加载进来的js脚本可能也是存在的。
建议你这样用:
document.scripts[1].innerHTML = " ";//取到对象,然后对这个对象进行操作。
------解决方案--------------------
可以,你把函数放在window.onload里执行即可!
------解决方案--------------------
<html>
<head>
<title> test </title>
<script language= "javascript " src= "1.js ">
</script>
<script language=javascript>
function RemoveScript()
{
var head=document.getElementsByTagName( "head ")[0];
var scriptObj=head.childNodes[1];
alert(head.childNodes.length); //输出3
head.removeChild(scriptObj);
alert(head.childNodes.length); //输出2,表示已经移出
}
</script>
</head>
<body>
<div onclick= "RemoveScript() "> check me </div>
</body>
</html>