我有这个HTML:
<div class="row" id="conditional-one">
<div class="large-12 columns">
<h3><?php echo $arrayStage_rule_one_title?></h3>
<p>
<?php echo $arrayStage_rule_one_description?>
</p>
<ul style="list-style-type:disc; margin-left:10px;">
<?php echo $arrayStage_rule_one_description_bullets?>
</ul>
<h3><?php echo $arrayStage_rule_one_bullets_title?></h3>
<br>
<ul style="list-style-type:disc; margin-left:10px;">
<?php echo $html_rule_one_bullets?>
</ul>
</div>
</div>
如果符合条件,我试图用id="conditional-one"
删除div内的所有HTML元素:
// select div by ID
var conditionalOne = document.getElementById("conditional-one");
// Assign PHP strings to JavaScript variables
var conditionalOneText = '<?php echo $place_bullet_one?>';
if (conditionalOneText !== "1" || conditionalOneText !== "2") {
conditionalOne.removeChild(conditionalOne.firstChild);
} else {
// do nothing
}
目前ConditionalOneText = = 3但conditional-one
内的div未被删除。如果条件满足,任何人都可以帮我擦除conditional-one
div中的所有HTML元素吗?
答案 0 :(得分:4)
只需将 innerHTML
设置为""
即可删除所有内容。
另外,如果你真的不想在你的else
块中做任何事情,那么你可以完全删除它,这将消除代码中可能存在的混淆。
// select div by ID
var conditionalOne = document.getElementById("conditional-one");
// Assign PHP strings to JavaScript variables
var conditionalOneText = '<?php echo $place_bullet_one?>';
if (conditionalOneText !== "1" || conditionalOneText !== "2") {
// You can remove all nested HTML content simply by setting
// the innerHTML property of the element to "".
conditionalOne.innerHTML = "";
}