如何在按下按钮时删除外部js文件

How to remove external js file on button press

提问人:Rarer 提问时间:6/19/2023 最后编辑:jcubicRarer 更新时间:6/27/2023 访问量:53

问:

   suraj.js
  var mins=.1;
    var secs=mins60;
    function countdown() {
        setTimeout('Decrement()',60);
    }
    function Decrement() {
        if(document.getElementById) {
            minutes=document.getElementById(minutes);
            seconds=document.getElementById(seconds);
            if(seconds59) {
                seconds.value=secs;
            }
            else {
                minutes.value=getminutes();
                seconds.value=getseconds();
            }
            if(mins1) {
                minutes.style.color=red;
                seconds.style.color=red;
            }
            if(mins0) {
                 alert('time up');
                minutes.value=0;
                seconds.value=0;
            }
            else {
                secs--;
                setTimeout('Decrement()',1000);
            }
        }
    }
 
    function getminutes() {
        mins=Math.floor(secs60);
        return mins;
    }
 
    function getseconds() {
        return secs-Math.round(mins60);
    }
!DOCTYPE html
html xmlns=httpwww.w3.org1999xhtml
head

!--LINK external js--
script type=textjavascript src=suraj.js data-scriptid=MyDomScriptscript

!-- Link jquery --
script type=textjavascript src=httpajax.googleapis.comajaxlibsjquery1.7.1jquery.min.jsscript

head
body onload=countdown()!-- Your web--
   
   button id=myBtnTry itbutton


 div style=display flex; width80%;
                justify-contentcenter; padding-top 0%;
        Time Left 
    div
    div style=display flex; width80%;
                justify-contentcenter; padding-top 0%;
        input id=minutes type=text style=width 3%; border none; font-size 16px;
                      font-weight bold; color black;
        font size=5
            
        font
        input id=seconds type=text style=width 3%; border none; font-size 16px;
                      font-weight bold; color black;
    div
    script type=textjavascript

document.getElementById(myBtn).addEventListener(click, function() {

let domScripts = document.querySelectorAll('[data-scriptid=MyDomScript]');
   domScripts.forEach(function(s) {
   s.remove();
});
});
script
body
html

我怎样才能在按下按钮时停止计时器,这将破坏js导入...还是任何手动方法?

我想从 DOM 中删除脚本标签并完全删除脚本已经定义的变量或事件。

JavaScript jQuery DOM 按钮 事件

评论

0赞 jcubic 6/27/2023
无法从已加载和执行的文件中删除 JavaScript 代码。您只能删除脚本标记,但不会删除正在运行的脚本。但是请描述您到底想做什么,因为这可能是 XY 问题
0赞 Rarer 6/29/2023
好的,我认为你的说法是实用的,因为删除方法无法访问渲染的方法,感谢您的澄清。
0赞 jcubic 6/29/2023
您只能删除添加到页面的 CSS,它将被删除,但使用 JavaScript,您不能。也许试着解释你想做什么,因为正如我所写的,你可能有 XY 问题,你问的是解决方案,而不是你遇到的问题。

答: 暂无答案