简体   繁体   English

JavaScript删除标记在Mozilla中不起作用

[英]JavaScript Remove Tag Not Working in Mozilla

I want to remove the tag <div id='parent'> if no data is stored in the <span id="test"> 如果没有数据存储在<span id="test">我想删除标签<div id='parent'> <span id="test">

It works perfectly in Chrome, but Firefox can not. 它可以在Chrome中完美运行,但Firefox不能。

Is there a solution to fix this code? 有解决此代码的解决方案吗? If you must use jQuery, I am also ready. 如果您必须使用jQuery,我也已经准备好了。

My Code: 我的代码:

<h1>Result:</h1>
<div id='parent'>
<b>Age:</b> <span id='test'></span>
</div>

<script>
if ( document.getElementById('test').innerHTML == '' ){
document.getElementById('parent').remove();
}
</script>

Sample Chrome: http://i.stack.imgur.com/lDesY.png Chrome浏览器示例: http//i.stack.imgur.com/lDesY.png

Sample Mozilla: http://i.stack.imgur.com/R2SS1.png 示例Mozilla: http//i.stack.imgur.com/R2SS1.png

replace this (see this example and this reference ) 替换它(请参见本示例和本参考

document.getElementById('parent').remove();

by 通过

currentNode = document.getElementById('parent').remove();
currentNode.parentNode().removeChild( currentNode );

If you are ready to use jQuery then you can do this 如果您准备使用jQuery,则可以执行此操作

if($("#test").html()==""){
    $("#parent").remove();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Result:</h1>
<div id='parent'>
<b>Age:</b> <span id='test'></span>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM