如何检查脚本src匹配,然后重新分配src

时间:2010-11-04 17:33:30

标签: javascript html

我需要检查每个script标记的src值,如果匹配,我想更改该脚本标记src属性......这样的事情:

var scripts = document.getElementsByTagName("script")[0].src

for (i=0;i<scripts.length;i++){
  if(scripts[i] == "something.js"){
    document.getElementsByTagName("script")[i].src = "this.js"
  }
  else {}
}} 

1 个答案:

答案 0 :(得分:8)

var scripts = document.getElementsByTagName("script");

for (i=0;i<scripts.length;i++){
  if(scripts[i].src == "something.js"){
    scripts[i].src = "this.js";
  }
}