I tried:
<script>
$(function() {
$('.class').before('<!--');
$('.class').after('-->');
});
</script>
but it didn't work for a reason unknown to me.
Can anyone help me understand why it didn't work and how I would do it? Thank you, much appreciated.
The reason to work jquery with static content is they are known to DOM, and can handle the events of known elements. but with dynamic contents you have to bind the event with that element by using .
$(document). ready(function() { $('body'). on('click', '. AddEl', function() { var $elems = $('.
jQuery comments are started with a double forward slash which tells the interpreter that the following text on the line is not to be processed as JavaScript. jQuery comments can be either inside a function or outside it makes no difference.
jQuery html() MethodThe html() method sets or returns the content (innerHTML) of the selected elements. When this method is used to return content, it returns the content of the FIRST matched element. When this method is used to set content, it overwrites the content of ALL matched elements.
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:
<h1>hello</h1>
<div>
<p>wow</p>
<p>dude</p>
</div>
JavaScript (+ jQuery):
$(document).ready(function () {
setTimeout(hideIt, 1000);
});
function hideIt() {
$('div').html('<!--' + $('div').html() + '-->');
}
You're mixing two completely unrelated concepts there: HTML and the DOM.
HTML is a textual means of describing content, which — like code — has a means of commenting things out. The DOM is the resulting object model in the browser.
Once the HTML has been read and parsed and the DOM has been created, the HTML is discarded and unnecessary. Adding HTML comments to things won't change them, and in fact doesn't make sense (although it's easy to see how you thought it would, don't get the wrong idea).
Once you have the DOM, to hide an element without removing it, use hide
. To remove it entirely, use remove
.
Live examples of both: http://jsbin.com/araju
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With