简体   繁体   English

如何使用JavaScript显示/隐藏范围

[英]How to show/hide a span using javascript

Can someone show a way to show/hide a span using javascript 有人可以使用javascript显示/隐藏跨度的方法吗

 document.getElementById("test").style.display= 'visible';
 document.getElementById("test").style.display= 'block';

In the HTML Code 在HTML代码中

<span id='test' ..

How can I overcome this problem. 我该如何克服这个问题。 Is there any thing that I should think about? 有什么我应该考虑的吗?

UPDATE I have a class like this one, I want to force mouse hovering on it. 更新我有一个像这样的类,我想强迫鼠标悬停在它上面。

<div id="test" class="tooltip effect">
        <div id="second" href="#"> .. </div>

On css: 在CSS上:

tooltip{..}
effect{..}
effect:hover{..}

Another option I tried besides your code is 除了您的代码,我尝试过的另一种选择是

document.getElementById("test").onmouseover = test.hover;
  • Should I re-write the hover class to another name-class, or should I tweak your code? 我应该将悬浮类重写为另一个名称类,还是应该调整代码?

Use display none/default: 使用无显示/默认:

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

Below are some types and some easy to remember rules about them: 以下是一些类型和一些容易记住的规则:

Default: the elements default property (generally block or inline) 默认值:元素的默认属性(通常为块或内联)

Block: Generally on a line by itself. 阻止:通常单独在线。 Has the width and height attributes (among other size/positioning attributes) 具有width和height属性(以及其他大小/位置属性)

inline: on the same line as other elements/text. 内联:与其他元素/文本在同一行。 Does not have height/width attributes 没有高度/宽度属性

Inherit: Inherits the parent element's display type 继承:继承父元素的显示类型

visible不是显示一个值,你想none

I would do something like this to handle it: 我会做这样的事情来处理它:

function HideAndSeek(selector) {

    var elements = undefined;
    var displays = [];

    if (!!selector.id) {
        elements = [document.getElementById(selector.id)];
    } else if (!!selector.class) {
        elements = document.getElementsByClass(selector.class);
    }

    for (var elementIndex = 0; elementIndex < elements.length; elementIndex++) {
        displays[elementIndex] = elements[elementIndex].style.display;
    }

    this.hide = function() {
        for (var elementIndex = 0; elementIndex < elements.length; elementIndex++) {
            elements[elementIndex].style.display = "none";
        }
    };

    this.show = function() {
        for (var elementIndex = 0; elementIndex < elements.length; elementIndex++) {
            elements[elementIndex].style.display = displays[elementIndex];
        }
    };
}

This function can be used this way: 可以通过以下方式使用此功能:

var hideAndSeek = new HideAndSeek({id: "test"});

and you can hide the element(s) by: 您可以通过以下方式隐藏元素:

hideAndSeek.hide();

you can show them by: 您可以通过以下方式显示它们:

hideAndSeek.show();

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

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