Is there any way to interact with HTML comments via JS?
<div><!--Камент-->простотекст</div> Ps do not say that it is better to use display: none. I'm just purely theoretically interested in the interaction with the comments.
Is there any way to interact with HTML comments via JS?
<div><!--Камент-->простотекст</div> Ps do not say that it is better to use display: none. I'm just purely theoretically interested in the interaction with the comments.
Here is an example. jquery there is not mandatory, so for imaginary convenience.
With your permission:
<div><!--comment 1-->text3<span>test</span><!--comment2--></div> Js:
$(document).ready(function(){ $("div").each(function(){ child = this.firstChild; while (child){ // determine the type of the node switch (child.nodeType){ // if the node is a comment node, output its value case Node.COMMENT_NODE : alert(child.nodeValue); break; } // move to the next child node child = child.nextSibling; } }); }); [pure_js] . - karmadro4[javascript] (just like that, not [js] ) and do not ignore (for example) [jquery] still read the question. Indeed, what kind of purism can we talk about, if in 95% of cases JavaScript is associated with a specific DOMImplementation? - karmadro4damn, did not have time:
$('div').contents().each(function(){ if(this.nodeType == Node.COMMENT_NODE) { console.log(this.data); } }); Source: https://ru.stackoverflow.com/questions/131180/
All Articles