Can you use jquery to add comments dynamically to code?

前端 未结 9 1475
无人共我
无人共我 2021-01-18 05:20

I tried:


but it didn

9条回答
  •  情话喂你
    2021-01-18 05:57

    It looks like you're trying to make objects with .class disappear. Use .hide() instead. Comments are only parsed when the browser first loads the page, so adding comments won't comment something out.

    You need to learn the difference between HTML and the DOM. HTML is the textual representation of the page, but the browser parses it into the DOM on page load. JavaScript works on the DOM, not on the HTML. Using .innerHtml() on DOM elements reparses the HTML.

    Here's an example of using innerHtml() to hide elements using HTML comments (but note that I would never do this - I'm only showing how to do what it looked like you were trying to do in your question):

    HTML:

    hello

    wow

    dude

    JavaScript (+ jQuery):

    $(document).ready(function () {
        setTimeout(hideIt, 1000);
    });
    
    function hideIt() {
        $('div').html('');
    }​
    

提交回复
热议问题