简体   繁体   English

如何在javascript中使用div隐藏标签?

[英]How will hide label using div in javascript?

I want hide a label according to a dropdown button. 我想根据下拉按钮隐藏标签。 I am using a div with JavaScript. 我正在使用带有JavaScript的div。 My code is: 我的代码是:

<div id="ime" name="ime">
    <h3 class="ms-standardheader">
        <label>
            <nobr>IEMI No</nobr>
        </label>
    </h3>
</div>

For hiding through JavaScript,which I am using: 为了通过JavaScript隐藏,我正在使用:

ime.style.display='none';
document.getElementById("ime").style.display='none';
document.getElementsByName("ime").style.display='none';

But this code is not working. 但是此代码不起作用。

On page load id to this div is not assigned that's way it is not working. 在页面加载ID到此div未分配,这是不起作用的方式。 Use this after body tag at the end of page. 在页面末尾的body标签之后使用此标签。

write this way 这样写

<html>
<body>
.
.
.
.
.
</body>
    <script type="text/javascript">
         document.getElementById('ime').style.visibility = 'hidden';
    </script>
</html>

What if you just use document.getElementById("ime").style.display='none'; 如果仅使用document.getElementById("ime").style.display='none'; without the other two lines (which will not work)? 没有其他两行(哪个行不通)?

I expect that the first line will throw an error, because ime is not defined, which will prevent execution of the next two lines. 我希望第一行将引发错误,因为未定义ime ,这将阻止后续两行的执行。

The third line will not work, because it returns an array of objects, which individually have a style attribute, but not together... 第三行不起作用,因为它返回一个对象数组,这些对象分别具有样式属性,但不能一起使用...

// this would work on the first `ime` tag
document.getElementsByName("ime")[0].style.display='none';

试试这个会起作用

document.getElementById("ime").style.display='none';

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

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