javascript 如何使按钮不显示但仍被javascript使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4490002/
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-10-25 13:52:29  来源:igfitidea点击:

How to make a button not show up but still be used by javascript?

c#javascriptasp.net

提问by Ram

I am using a button that has to be invible and should be used by a javascript function.

我使用的按钮必须是不可见的,并且应该由 javascript 函数使用。

<asp:Button ID ="btnDummy1" runat="server" Visible ="true" OnClick="btnSubmit1_Click" width="0px" height="0px"/

I cannot keep visible = false as it the javascript will not use invible content in the poage. I havetried to give width=0 and height=0, still it showws up in Chrome. What do you guys think i should do?

我不能保持 visible = false 因为它 javascript 不会在 poage 中使用不可见的内容。我已经尝试给 width=0 和 height=0,它仍然显示在 Chrome 中。大家觉得我应该怎么做?

Thanks in advance :)

提前致谢 :)

回答by Nick Craver

A pretty clean approach in ASP.Net it give it a "hidden" class:

ASP.Net 中一种非常干净的方法,它给了它一个“隐藏”类:

<asp:Button ID ="btnDummy1" runat="server" CssClass="hidden" />

Then in your stylesheet:

然后在您的样式表中:

.hidden { display: none; }

回答by rtpHarry

If you set it to Visible="False"then the code will not be executed.

如果你设置它,Visible="False"那么代码将不会被执行。

Instead I think you should wrap it in a and set display:nonevia css:

相反,我认为您应该将它包装在 a 中并display:none通过 css设置:

<div style="display: none;">
   <asp:Button ID ="btnDummy1" runat="server" OnClick="btnSubmit1_Click" />
</div>

回答by Bala R

How about

怎么样

style="display:none"

for the button instead of Visible = "true".

对于按钮而不是 Visible = "true"。

回答by mike

Can you just use a hidden form field in this case? This is generally the preferred method of passing information along.

在这种情况下,您可以只使用隐藏的表单字段吗?这通常是传递信息的首选方法。

See http://www.tizag.com/htmlT/htmlhidden.php

http://www.tizag.com/htmlT/htmlhidden.php

回答by BIJI LAOCH

adding style="display:none" would hide the button till you make it visible again

添加 style="display:none" 会隐藏按钮,直到你让它再次可见

<asp:Button ID ="btnDummy1" runat="server"  OnClick="btnSubmit1_Click" style="display:none"/>

回答by Nicolas

I think the first question you should ask yourself is : why would I put in my HTML doc a button that should not be visible ?

我认为您应该问自己的第一个问题是:为什么我要在我的 HTML 文档中放置一个不应该可见的按钮?

An HTML document should be used ONLY TO DESCRIBE A CONTENT'S SEMANTICS. So an HTML doc should ONLY contain the content you want to publish and extra data to explain the content's SEMANTIC !! NOTHING about the way it is displayed, NOTHING about the way it behaves !!

HTML 文档应该仅用于描述内容的语义。所以一个 HTML 文档应该只包含你想要发布的内容和额外的数据来解释内容的语义!!与它的显示方式无关,与它的行为方式无关!!

All displaying and behaviors questions should be managed with CSS and Javascript, NEVER within HTML itself.

所有显示和行为问题都应使用 CSS 和 Javascript 进行管理,切勿在 HTML 本身中进行管理。

Any element needed for Javascript only purpose should be added in the doc by Javascript itself !

任何仅用于 Javascript 目的所需的元素都应由 Javascript 本身添加到文档中!

You should never find, in an HTML doc, such things as prev/next buttons for a javascript picture gallery for example. The HTML doc should only contain the pictures list, than your JS script will change the way this list is shown, making it a eye candy gallery and add buttons for navigation.

例如,您永远不应该在 HTML 文档中找到诸如 javascript 图片库的上一个/下一个按钮之类的东西。HTML 文档应该只包含图片列表,而不是您的 JS 脚本将更改此列表的显示方式,使其成为一个令人眼花缭乱的画廊并添加导航按钮。

So in your example, your saying you want to add in your HTML doc an invisible button with some Javascript actions on it.... that's probably an example of some content that should never be in the HTML doc.

所以在你的例子中,你说你想在你的 HTML 文档中添加一个不可见的按钮,上面有一些 Javascript 操作......这可能是一些不应该出现在 HTML 文档中的内容的例子。