简体   繁体   English

为什么$('body')。html(“”)无法清除调用它的JavaScript?

[英]Why doesn't $('body').html(“”) clear the javascript that calls it?

This is a piece of code that links to a javascript file that generates some fake tweets, along with some self made comments that attempt to explain to myself what is happening. 这是一段代码,链接到一个javascript文件,该javascript文件生成一些虚假的tweet,以及一些试图向自己解释正在发生的事情的自制注释。 I was wondering what the purpose of $body.html(''); 我想知道$body.html('');的目的是什么$body.html(''); was. 是。 It seems like it just clears the contents of the body, which is already empty except for the javascript that is present. 似乎它只是清除正文的内容,除了已存在的javascript之外,该内容已经为空。 Wouldn't this also clear the actual script that inside the body as well? 这难道还不能清除体内的实际脚本吗? ie, why doesn't the whole script just vanish when we reach that line. 也就是说,当我们到达那一行时,为什么整个脚本不会消失。 I'm guessing that the function is executed in its entirety before the body is cleared? 我猜想该功能是在清除主体之前完整执行的吗? Just looking for a little illumination, I guess, on function execution. 我想,只是在功能执行上需要一点点启发。

<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script src="data_generator.js"></script>
</head>
<body>
 <script>


  $(document).ready(function(){   // calls function only after dom is loaded                        
    var $body = $('body');        // selects html body tag, stores in $body
    $body.html('');               // clears body?

    var index = streams.home.length - 1;   // sets index to length of streams array
    while(index >= 0){
      var tweet = streams.home[index];     // gets a tweet string
      var $tweet = $('<div></div>');       // $tweet is a div element
      $tweet.text('@' + tweet.user + ': ' + tweet.message);  // add formatted tweet to div
      $tweet.appendTo($body);             // add tweet to body
      index -= 1;                         // rinse, repeat
    }

  });

 </script>
</body>
</html>

EDIT: To be clear, I didn't write the code, only the comments. 编辑:要清楚,我没有写代码,只有注释。 I am just trying to break it down and understand each line. 我只是想将其分解并理解每一行。

When the Javascript is run the function context is stored in memory, and closed-over variables are stored in the context. 运行Javascript时,函数上下文存储在内存中,封闭变量存储在上下文中。 They don't go away. 他们不会消失。 Globals are attached to window . 全局变量附加到window They don't go away either. 他们也不会消失。

I think it is a better idea to append the script to the header (where all the other scripts reside) than to the body. 我认为将脚本附加到标头(所有其他脚本所在的位置)比正文更好。

If you want to completely eliminate the script that is being called, you could use: 如果要完全消除正在调用的脚本,可以使用:

<script>
(function foo(){
    var b=function moo(){
        var c=document.getElementsByTagName('script');
        alert(document.body.innerHTML);
        c[0].parentElement.removeChild(c[0]);
        alert(document.body.innerHTML);
    }
    var a=setTimeout(b,1000);
    b=null;
})();
foo=null;
</script>

Keep in mind, that will COMPLETELY remove any functionality of the script and any reference to it in the DOM . 请记住,这将完全删除script任何功能以及在DOM对其的任何引用。

Basically, because the script has already loaded, you can't just remove it from the DOM . 基本上,因为脚本已经加载,所以不能仅仅remove其从DOM remove

Is there really any reason to get rid of it, now that it's already loaded...??? 现在已经加载了,真的有什么理由要摆脱它吗????

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM