禁用 JavaScript 时如何隐藏部分 HTML?
我正在尝试适应没有 JavaScript 的用户。 (顺便问一下,现在值得付出努力吗?)
在一个 HTML 文件中,如果脚本打开,我希望执行其中的一部分,如果脚本关闭,则执行另一部分。
标签让我可以做到前者,但如何实现后者呢?如果脚本关闭,如何标记 HTML 的一部分,使其不会被浏览器解析?
I'm trying to accommodate users without JavaScript. (By the way, is it worth the effort nowadays?)
In one HTML file I'd like part of it execute if scripts are on, and another part if scripts are OFF.
The <noscript>
tag lets me do the former, but how to achieve the latter? How can I mark a part of HTML so that it is not parsed by browser if scripts are off?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我一直在寻找一种方法来做到这一点,这样我就可以隐藏一个导航下拉菜单,当启用 JavaScript 时,该菜单会变得不起作用。然而,所有改变显示属性的解决方案都不起作用。
因此,我首先为下拉菜单周围的 div 元素分配了一个 ID (ddown)。
然后,在 HTML 文档的 head 部分中,我将其添加到:
这样就起作用了。不依赖 javascript、jquery 或任何其他脚本:仅是纯 HTML 和 CSS。
I had been looking around for a way to do this so that I could hide a navigation dropdown menu that gets rendered nonfunctional when javascript is enabled. However, all of the solutions for changing the display property did not work.
So, what I did first was assigned an ID (ddown) to the div element surrounding the dropdown menu.
Then, within the head section of the HTML document, I added this in:
And that just worked. No dependency on javascript, jquery, or any other scripting: just pure HTML and CSS.
默认将您想要隐藏的位设置为
display:none
样式,并使用 jQuery 或 JavaScript 将其打开。Default the bits you want to hide as styled as
display:none
and switch them on with jQuery or JavaScript.这里有一个关于如何使用 jQuery 完成此操作的视频教程:http://screenr.com/ya7代码:
然后相应地隐藏
body.noscript
下的相关元素即可。编辑
然而,对于像这样的小修复,JQuery 可能会显得臃肿,所以我建议 Zauber Paracelsus 的答案,因为它不需要jQuery。
here's a video tutorial on how this can be done with jQuery: http://screenr.com/ya7Code:
And then just hide the relevant elements under
body.noscript
accordingly.edit
However, JQuery might be bloated for a small fix like this one, so I suggest Zauber Paracelsus' answer since it does not require JQuery.
是的,可访问性值得担心。您应该尽可能使用渐进增强,即制作一个无需脚本即可运行的基本版本,然后在其之上添加脚本功能。
一个简单的例子是弹出链接。您可以像这样制作一个:
但是,如果有人单击鼠标中键或尝试为其添加书签,或者禁用了脚本等,则此链接将无法工作。相反,您可以这样做相同的事情:
它仍然不完美,因为您应该将行为层和结构层分开并避免内联事件处理程序,但这样做更好,因为它更易于访问并且不需要编写脚本。
Yes, it's worth worrying about accessibility. You should use progressive enhancement where possible, i.e. make a basic version that works without scripting and then add the scripting functionality on top of it.
A simple example would be a pop-up link. You could make one like this:
However, this link wouldn't work if someone, say, middle-clicked or tried to bookmark it, or had scripts disabled etc. Instead, you could do the same thing like this:
It's still not perfect, because you should separate the behavior and structure layers and avoid inline event handlers, but it's better, because it's more accessible and doesn't require scripting.
如果内容仅在启用 JavaScript 时才有意义,则应由 JavaScript 代码直接插入而不是渲染。这可以通过简单地将 HTML 模板作为 JavaScript 代码中的字符串来完成,或者如果 HTML 更复杂,则可以使用 Ajax 来完成。
正如 Reinis I 提到的。 这是渐进增强的想法。
关于在 body 标签上使用类名的 CSS 技术,我建议以相反的方式执行此操作,并使用 JavaScript 将“
js-enabled
”类添加到 body 标签,这将改变页面CSS。这符合我上面关于保持所有初始 HTML“非 JavaScript 友好”的评论。If the content only makes sense when JavaScript is enabled, then it should be inserted by the JavaScript code directly rather than being rendered. This could either be done by simply having HTML templates as strings within your JavaScript code, or using Ajax if the HTML is more complex.
As mentioned by Reinis I. this is the idea of Progressive Enhancement.
Regarding the CSS techniques of using a class name on the body tag, I would advise doing this the other way around and adding a '
js-enabled
' class to the body tag with JavaScript that would alter the page CSS. This fits in with my above comment about keeping all initial HTML 'non-JavaScript friendly'.jQuery 方式:
伪 CSS
HTML
The jQuery way:
Pseudo CSS
HTML
您可以使用 Javascript 编辑页面的 HTML
Try it with JS and the without JS.
You could edit the HTML of the page with Javascript
Try it with JS and the without JS.
这使得 div 内的内容在没有 JS 的情况下无法访问,但使用 JS 时,它会使用 JS 将内容添加到 div 中,您也可以使用 CSS 来做到这一点。
要更改 CSS 添加:
This makes the content inside the div inaccessible without JS but with JS it adds the content into the div with JS and you can also do that with the CSS.
To change the CSS add: