<div id="test">subject</div>
var d=document.getElementById('test');
d.outerHTML='<!-- id="test" -->';
d.outerHTML='<div>test</div>';
but there is error:Failed to set the ‘outerHTML’ property on ‘Element’:
I can set it to but can not set it again.
I want to learn vitrual dom concept.
<div id="test" for="my_items">subject</div>
if the items
is:
var items=[{value:'sub'},{vallue:'math'}];
the html will be:
<div id="test_sub"></div><div id="test_math"></div>
when items
change I can edit it with the document.getElementById.
and set the outerHTML.
if the items
is:
var items=[];
there will no html;
how to update them when there is some items initems
again? there is nothing.
you may say I can create a tree to update it .but I wonder if I can do it by not create a tree.
so I am asking if I can create <!-- id="test" -->
and when changed I can update the <!-- -->
outerHTML?
2
Answers
If you are using Vue.js, there are more declarative ways to conditionally render elements, and these ways are often more concise and expressive compared to directly manipulating the DOM using plain JavaScript.
Vue.js provides directives like
v-if
,v-else
,v-show
, and others, which can be used to conditionally render elements based on the state of the component or application.Something like:
In Vue.js, when the data properties of a component change, the component re-renders the parts of the template that are dependent on those properties. So, if the items array changes, the component will automatically update the DOM to reflect those changes.
For instance, you can add items with a button:
It is not possible to directly insert items into a comment in HTML using standard DOM methods. Comments are not considered part of the Document Object Model (DOM) tree in the same way that HTML elements are, therefore they cannot be selected or manipulated using the usual DOM methods.
Instead, you can use comments as a visual marker in the HTML code to indicate the desired location for adding elements.
Here is an example of how you can use JavaScript to dynamically manipulate the DOM based on an array of data: