Javascript 在 HTML 中隐藏和显示跨度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10353013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:00:12  来源:igfitidea点击:

Hiding and showing span in HTML

javascriptjsphtml

提问by Shantanu Tomar

I have a JSP page on which i have some input submitbuttons. Now based on some values i get from an AJAXrequest i want to control the display and hiding of these input buttons. So i have created spansfor each input. And based on the variable value i received from AJAX request i manipulate there display property. But i am not getting the results right.

我有一个 JSP 页面,上面有一些input submit按钮。现在基于我从AJAX请求中获得的一些值,我想控制display and hiding of these input buttons. 所以我spans为每个输入创建了。并根据我从 AJAX 请求收到的变量值,我操纵那里的显示属性。但我没有得到正确的结果。

here's my code:

这是我的代码:

<td style="width: 600px"><span id="startspan"><input name="start" value="startActivity" type="submit" id="startbuttonid"></span></td>
       <td style="width: 600px"><span id="holdspan"><input name="start" value="holdActivity" type="submit" id="holdbuttonid"></span></td>
       <td style="width: 600px"><span id="cancelspan"><input name="start" value="cancelActivity" type="submit" id="cancelbuttonid"></span></td>
       <td style="width: 600px"><span id="closespan"><input name="start" value="closeActivity" type="submit" id="closebuttonid"></span></td>

And my java scriptcode where i am writing code for display or hiding them is ::

而我的java script代码我在哪里写代码显示或隐藏他们是::

if(temp1[15]=="InProcess"){
        document.getElementById('startspan').style.display='none';
        document.getElementById('holdspan').style.display = 'block';
        document.getElementById('cancelspan').style.display = 'block';
        document.getElementById('closespan').style.display = 'block';
    }
    if(temp1[15]=="New"){
        document.getElementById('startspan').style.display='block';
        document.getElementById('holdspan').style.display = 'none';
        document.getElementById('cancelspan').style.display = 'block';
        document.getElementById('closespan').style.display = 'none';
    }

here based on variable temp1[15]value which i am receiving fine. I want to display or hide those input submit buttons. I am doing it the right way, like defining span or need some correction. Basically all those input buttons are inside a dialog box <div>which opens up only when a function is triggered inside which i have written my Hiding or showing span code(Written above). Need help. Thanks.

这里基于temp1[15]我收到的变量值。我想显示或隐藏那些输入提交按钮。我正在以正确的方式进行操作,例如定义跨度或需要一些更正。基本上所有这些输入按钮都在 adialog box <div>内,只有当我在里面写了我的隐藏或显示跨度代码(写在上面)的功能被触发时才会打开。需要帮忙。谢谢。

回答by Andrea

The displayproperty in CSS also determines whether it acts like a block (DIV, P, etc), or an inline element (span, b, etc), as well as its visibility.

displayCSS 中的属性还决定了它是像块(DIV、P 等)还是内联元素(span、b 等),以及它的可见性。

For your spans you want to do this instead:

对于您的跨度,您想要这样做:

document.getElementById('span').style.display = 'inline';

If it was a DIV you would use block, and if it was an IMG you would use inline-block.

如果它是一个 DIV,你会使用block,如果它是一个 IMG,你会使用inline-block

回答by AnalogWeapon

Since it is working in the fiddle and not in your main code, I suspect that there might be an issue related to your code executing before the DOM is loaded (or the dialog box <div>is generated). You mention that you're using AJAX. Make sure that the code to show or hide the buttons is in the AJAX success callback.

由于它在小提琴中工作而不是在您的主代码中工作,我怀疑可能存在与您的代码在加载(或dialog box <div>生成)之前执行的代码相关的问题。您提到您正在使用 AJAX。确保显示或隐藏按钮的代码在 AJAX 成功回调中。