简体   繁体   English

如何在禁用Javascript的情况下让我的UI“优雅地降级”?

[英]How do I make my UI 'degrade gracefully' with Javascript disabled?

I've read in multiple posts on SO that if users have Javascript disabled, ideally your page should 'degrade gracefully'. 我已经在SO上阅读了多篇文章,如果用户禁用了Javascript,理想情况下,您的页面应该“优雅地降级”。 I'm not sure in general what types of things should be done to make this happen. 我不确定一般来说应该做些什么类型的事情来实现这一目标。

I have a blob of HTML for configuring a 'schedule'. 我有一大堆HTML用于配置'计划'。 Depending on the value of a select box, different fields are shown. 根据选择框的值,将显示不同的字段。

<select name="schedule.frequency"
    id="schedule.frequency" 
    onChange='updateScheduleFields()' >
        <option value="Manual">Run Manually</option>
        <option value="Monthly">Monthly</option>
        <option value="Weekly">Weekly</option>
        <option value="Daily">Daily</option>
        <option value="Hourly">Hourly</option>
</select>

When the select is updated, I hide the fields that are shown so that things like 'day of the week' or 'days of the month' aren't shown when it's inappropriate. 更新选择后,我隐藏显示的字段,以便在不合适时不显示“星期几”或“月中的日期”等字段。 I'm not really sure how to make this degrade gracefully without Javascript, because as stated, some of the fields are just completely inappropriate for various schedule types. 我不确定如何在没有Javascript的情况下优雅地降低这种性能,因为如上所述,某些字段对于各种计划类型来说完全不合适。

My question is: In general, how would I make something that disables/hides inappropriate fields or does some pre/post processing degrade properly without Javascript? 我的问题是:一般来说,如果没有Javascript,我将如何制作禁用/隐藏不适当字段的内容或某些前/后处理会降级?

I'd also be interested in specific sites that I could look at that handle this type of degradation well? 我也对我能看到的可以处理这种退化的特定网站感兴趣吗?

The basic idea is that you have a standard (non-js) way of doing something, and then you add JavaScript to override that default behaviour. 基本思想是你有一个标准(非js)的做事方式,然后你添加JavaScript来覆盖默认行为。

In your case, you have a select , so the non-js behaviour would be that when you select an option, you submit a form which loads a new version of the page with different fields shown. 在您的情况下,您有一个select ,因此非js行为将是当您选择一个选项时,您提交一个表单,该表单加载显示不同字段的页面的新版本。

Your JavaScript (if enabled) will hide the submit button and do the updating in-place. 您的JavaScript(如果已启用)将隐藏提交按钮并进行就地更新。

The preferred way of thinking about it is progressive enhancement , and it's much easier to achieve that way round. 思考它的首选方式是渐进增强 ,并且更容易实现这种方式。 Make your page work as barebones HTML, then progressively enhance it with CSS and JavaScript. 使您的页面作为准系统HTML工作,然后使用CSS和JavaScript逐步增强它。 Then you never have to work out if it will degrade gracefully. 然后,如果它会优雅地降级,你永远不必解决。

It's easier to do progressive enhancement if your JavaScript and CSS are in separate files rather than inline. 如果您的JavaScript和CSS在单独的文件而不是内联中,则更容易进行渐进增强。 With an Ajax library like jQuery*, you can select elements with CSS selectors in order to add behaviour to those elements. 使用像jQuery *这样的Ajax库,您可以使用CSS选择器选择元素,以便为这些元素添加行为。 In your case, you'd do $('#schedule_frequency').change(updateScheduleFields) (I don't think you should have a period in your id). 在你的情况下,你会做$('#schedule_frequency').change(updateScheduleFields) (我不认为你的id应该有一段时间)。

* Disclaimer: It is possible to select HTML elements in JavaScript without a library, and Ajax libraries other than jQuery exist. *免责声明:可以在没有库的情况下在JavaScript中选择HTML元素,并且存在除jQuery之外的Ajax库。

When you think about sites degrading gracefully, think about how you would use the site without any javascript. 当您考虑网站优雅地降级时,请考虑如何在没有任何JavaScript的情况下使用该网站。 Then use javascript and the onload/ready hook to change elements of your site into what you originally wanted with javascript. 然后使用javascript和onload / ready挂钩将您网站的元素更改为您最初想要的javascript。 This way, if javascript is disabled, the site works. 这样,如果禁用了javascript,该网站就可以运行。 If it is enabled, the setup javascript will run and transform the page. 如果启用,则设置javascript将运行并转换页面。

Usually this means having an extra "submit" button that your setup javascript hides or something like that. 通常这意味着有一个额外的“提交”按钮,你的设置javascript隐藏或类似的东西。

What I would do in this case is have a "Next" button below the drop-down list that posts the selection to the server. 在这种情况下我要做的是在下拉列表下方有一个“下一步”按钮,用于将选择发布到服务器。 Then you would need server-side logic to show/hide the fields. 然后,您需要服务器端逻辑来显示/隐藏字段。 I usually would get it working this way first, and then go back and add Javascript flair to hide the button and prevent the round-trip. 我通常会首先以这种方式工作,然后返回并添加Javascript天赋以隐藏按钮并防止往返。

I believe the short answer is that you won't be doing those types of things. 我相信简短的回答是你不会做那些类型的事情。 Run-time modification to the rendered page is achieved via JavaScript - when it is disabled you don't get to use those kinds of effects. 对渲染页面的运行时修改是通过JavaScript实现的 - 当它被禁用时,你不会使用这些类型的效果。

Additionally, you won't be doing pre-post processing without JavaScript. 此外,如果没有JavaScript,您将不会进行事后处理。 You'll need to implement your checking / testing in the back end. 您需要在后端实施检查/测试。

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

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