简体   繁体   English

jQuery和动态元素vs显示css属性

[英]jQuery and dynamic elements vs display css attribute

While designing html interface elements it's a pretty common scenario having DOMs show/hide depending on certain events. 在设计html界面元素时,根据某些事件显示/隐藏DOM是一种非常常见的场景。 My question is simple, but every time I run into this I always wonder: is it "better" to have the elements hardcoded in the html and simply switch the display attribute (eg with .show/.hide) or dynamically add/remove them as needed via JS? 我的问题很简单,但每次遇到这个问题我总是想知道:在html中硬编码元素并简单地切换display属性(例如使用.show / .hide)或动态添加/删除它们是“更好”的吗?根据需要通过JS? Both are trivial to implement but I can't help but wonder how do they compare and whether there's any advantage/drawback of using one over the other. 两者都是微不足道的实现,但我不禁想知道它们如何比较,以及使用一个优于另一个是否有任何优点/缺点。

There are cases, such as when the same element is used verbatim in several places, where it seemingly makes sense to create the DOMs dynamically as you go, but on the other hand hardcoding them theorically is more maintainable in that you can move the DOMs around and change them as you need and they will work as expected as long as the selector is still the same for jQuery. 有些情况,例如在几个地方逐字使用相同的元素,在你去的时候动态创建DOM似乎是有意义的,但另一方面,理论上对它们进行硬编码更加可维护,因为你可以移动DOMs并根据需要更改它们,只要选择器对于jQuery仍然相同,它们将按预期工作。 Generally speaking from a designer prespective it seems like hardcoding is the way to go, but I look forward for thoughts and perhaps things I may be overlooking here. 一般来说,从设计师的角度来看,硬编码似乎是要走的路,但我期待着思考,也许我可能会在这里看到的东西。

Edit: By "hardcoded" I meant elements that are not inserted via JS at all; 编辑:通过“硬编码”,我指的是根本不通过JS插入的元素; that is, elements that have their position already designated within its parent document in the original html markup rather than by JS. 也就是说,已经在原始html标记中的父文档中指定其位置的元素而不是JS。

performance needs to be considered when working on bigger apps, and when there is no much performance concern while coding smaller ones do what you feel easy and manageable. 在处理更大的应用程序时需要考虑性能,并且在编写较小的应用程序时没有太多性能问题时,请执行您认为简单易用的操作。 Very big DOM might kill mobile browsers. 非常大的DOM可能会杀死移动浏览器。 A lot of DOM modification can affect performance. 很多DOM修改都会影响性能。 In bigger applications it is better to keep DOM light and also reduce DOM modifications. 在更大的应用程序中,最好保持DOM光并减少DOM修改。 So in a nutshell it's kind of convenience over convention when performance is not closely watched. 因此,简而言之,当性能未受到密切关注时,它比传统方便。 it is but always recommended to keep a watch on performance and good practices. 但始终建议您密切关注性能和良好做法。

Ultimately, it really depends on what you're working with and what your application does, but: 最终,它实际上取决于您正在使用的内容以及您的应用程序的功能,但是:

  • Static markup means more work for the server (but only if the page is generated dynamically in the first place, for static pages the effect is negligible). 静态标记意味着更多的服务器工作(但只有在页面首先动态生成,对于静态页面,效果可以忽略不计)。

  • Dynamic markup means more work for the client , which is not always a bad thing, depending on your target audience. 动态标记意味着为客户提供更多工作,这并不总是坏事,具体取决于您的目标受众。

Technically, however, Graceful Degradation suggests that static markup is the way to go, as ideally the page should remain usable with scripting disabled (dynamic markup being an optional layer above a working static core). 但从技术上讲, Graceful Degradation表明静态标记是要走的路,因为理想情况下,页面应该在禁用脚本时保持可用(动态标记是工作静态核心上方的可选层)。

On the other hand, Progressive Enhancement builds from there and basically says that since we're already working with a bare-bones core, we can fire on all cylinders client-side, and dynamically enhance the static markup with content that takes advantage on the latest browsers' technologies. 另一方面, Progressive Enhancement从那里构建并基本上说,因为我们已经在使用一个简单的核心,我们可以在客户端的所有柱面上启动,并动态增强静态标记,内容利用了最新浏览器的技术。

For instance, jQuery UI and jQuery Mobile are designed around progressive enhancement, as their strategy is to extend existing markup with richer client-side behavior, generally exposed through dynamically created elements. 例如, jQuery UIjQuery Mobile是围绕渐进增强而设计的,因为他们的策略是扩展现有标记,使客户端行为更丰富,通常通过动态创建的元素公开。

So, both options in your title can and arguably should be implemented together. 因此,标题中的两个选项都可以并且可以说应该一起实现。 In the general case, and if possible, rendering the markup necessary for your application to work on a scriptless platform, then enhancing that markup from the client side looks like a good idea. 在一般情况下,如果可能的话,渲染应用程序在无脚本平台上工作所需的标记,那么从客户端增强该标记看起来是个好主意。

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

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