内容、表现和行为每次都可能分离吗?
内容、表现和行为每次都可能分离吗?
很多时候,我们通过 javascript 将类添加到 html 中,并在 CSS 中使用 visibility:hidden inline
。
我们是否应该始终投入时间将所有内容分开?
Is separation of Content, presentation and behavior possible every time ?
Many time through javascript we add classes to html and we use in CSS visibility:hidden inline
.
Should we always invest time to keep all separate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然这通常是可能的,但通常不切实际。正如您自己所说,这需要时间。你是否愿意投入时间取决于很多因素,纯粹主义只是其中之一。
事实上,可以说(我亲眼目睹了这一点),因为
div
或span
等标签在以及它们本身,但它们是专门为样式而发明的,当您在页面上使用其中一个时,您就已经在混合内容和演示了。这是一个很好的哲学讨论,但在商业环境中它没有任何结果。因此,虽然您应该始终尝试尽可能地将内容、表示和行为分开,但您总是必须在某个地方停下来,即使只是为了完成其他事情。
While it is usually possible, it is often not practical. As you say yourself, it costs time. Whether you are willing to invest that time is subject to many factors, of which purism is only one.
In fact, it can be argued (and I've witnessed it being argued first-hand) that since tags such as
div
orspan
don't carry any semantical meaning in and on themselves, but have been invented specifically for styling, the moment you use a single one of them on your page, you are already mixing content and presentation. This is a nice philosophical discussion to have, but in business context it leads nowhere.So, while you should always try to separate content, presentation and behavior as much as possible, you will always have to stop somewhere, if only to get other things done.
你的问题太笼统了,我们无法给你一个明确的答案。
但是,没有什么是不可能的,我想说,是的,这是可能的。
同时,我想说:
Your question is quite too general for us to give you a definite answer.
But, as nothing is impossible, I would say that, yes, it is possible.
In the same time, I would say that :
没有过多讨论的一件事是如何使样式远离您的行为 - 也就是说,像
$('element').css({color: 'purple'})
这样的样式内容只要有可能,就不用你的 Javascript 了。出于代码美观以及一般理智的原因,我发现最好让代码通过更新“类”值来更改样式。换句话说,不要认为代码中的某些东西应该是“紫色的”——认为它应该是“富丽堂皇的”或“独特的”或“有点丑陋的”:然后你的 CSS 可以接管:
现在你'只要您愿意,您就可以随意对样式进行一些“有点丑陋”的调整,而不必弄乱您的 Javascript。将字体变大,将其隐藏,等等;代码中的逻辑和操作保持不变。
同样,您可以通过在 CSS 中使用父/子类关系来完成一些很棒的事情,以保持您的行为简单、快速且无样式。
One thing that isn't talked about too much is how to keep style out of your behavior - that is, style stuff like
$('element').css({color: 'purple'})
out of your Javascript whenever possible. For reasons of code aesthetics as well as general sanity, I've found that it's way better to have code make style changes by having it update "class" values. In other words, don't think in the code that something should be "purple" — think that it should be "regal" or "distinctive" or "kind-of-ugly":Then your CSS can take over:
Now you're free to make little style tweaks to "kind-of-ugly" whenever you want without having to mess up your Javascript. Make the font bigger, make it hidden, whatever; the logic and the action in the code stays the same.
Similarly, you can do awesome things by using parent/child class relationships in CSS to keep your behaviors simple, fast, and style-free.
我认为这是概念上的分离,是的。
I think that as conceptual separation, yes.