jQuery - 使用隐藏/显示来切换内容,以便在启用 JavaScript 时显示某些内容,在 *禁用* 时显示其他内容?
我正在尝试使用 jQuery 隐藏 no_js 类的内容,并使用 js 类显示内容。我将其用于表单上的按钮;当启用 Javascript 时,我显示将 onclick
绑定到 Javascript 函数的 type="button",当禁用 Javascript 时,页面显示(默认显示)type="submit “
按钮。这是我到目前为止所得到的:
<input class="no_js" type="Submit" value="Generate"/>
<input class="js" type="button" value="Generate_JS" onclick="generate_password()"/>
.js 元素在样式表中设置为 display:none
。我想使用隐藏/显示或切换,因为元素可能有不同的显示类型(inline
、inline-block
,这取决于元素),所以我不能只需使用 display 设置 CSS 属性(并且此脚本是站点范围的,因此它基于类,而不是 ID)。
有人可以帮我吗?现在,“生成”按钮始终显示,而“生成_JS”按钮从不显示。
编辑:抱歉,我想我不能在评论中添加代码?这是我在标题中使用的代码,结果仍然相同。
$(document).ready(function() {
$(".no_js").hide();
$(".js").show();
})
I'm trying to use jQuery to hide content with class no_js, and show content with class js. I use this for buttons on a form; when Javascript is enabled, I show the type="button" that binds onclick
to a Javascript function, and when Javascript is disabled, the page shows (shown by default) the type="submit"
button. Here's what I have so far:
<input class="no_js" type="Submit" value="Generate"/>
<input class="js" type="button" value="Generate_JS" onclick="generate_password()"/>
.js elements are set to display:none
in the style sheet. I wanted to use hide/show or toggle, because the elements may have different display types (inline
, inline-block
, it depends on the element) so I can't just set the CSS properties using display (and this script is site wide, so it's based on class, not ID).
Can someone please help me? Right now, the Generate button shows all the time, and the Generate_JS button never shows.
EDIT: Sorry, I guess I can't put code in my comments? This is the code I'm using in my header, and the results are still the same.
$(document).ready(function() {
$(".no_js").hide();
$(".js").show();
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以使用
元素,该元素仅在当前所有使用 javascript 的浏览器中未检测到脚本语言时才会显示。
You can also use the
<noscript>
element, which will only display when no scripting language is detected, in all of the current browsers that would be javascript.该问题是由我的网络主机系统中的不相关错误引起的;他们在通过的大多数数据包上都遇到了数据包丢失的情况,在所有这些混乱中,我上传的 JQuery 库没有通过。我原来的代码又可以工作了。
The problem was caused by an unrelated error in my web host's system; they were experiencing packet loss on the majority of packets that went through, and in all that mess, the JQuery library that I uploaded hadn't made it through. My original code is working again.