如何向 jquery 插件添加结构样式
我刚刚编写了一个 jquery 插件,可以对表进行操作(如网格插件)。
该插件向单元格添加了一些样式,例如 overflow:hidden;
以及表格和单元格的边框(为了保持统一的边框宽度,我添加了: .MyTable {border-width:0 0 1px 1px;}
和 .MyTable > td {border-width:1px 1px 0 0 }
现在我想知道如何以及在哪里放置此 css 。样式。
我可以将它们放在外部 css 文件中,但随后我强制用户将 .css 文件添加到她的页面中。为了使所有用户(甚至是新用户)都可以访问该插件,这可能是一个障碍。用户可能会忘记添加 css 文件,甚至更改其内容以实现某些目的,这将使插件无法正常工作。
此外,我想让用户能够更改某些内容(例如边框的宽度)。如果我将 CSS 放在外部文件中,用户可以自己更改她想要的任何内容。
另一种选择是,我还可以直接在 jquery elem 上分配样式(使用 elem.css(...) 命令)。这样,用户不必处理 css 文件即可正常工作,而以另一种方式,为了允许更改样式(如边框),我可以向插件添加一个选项。但是,这样我就创建了低效的代码,并且在行为和设计之间进行了融合。
是否有其他可能向 jquery 插件添加样式?
最好的方法是什么?
我不是在谈论应该确定颜色或字体的样式,而是我制作的结构所必需的样式!
I just wrote a jquery plugin that makes operations on a table (like grid plugin).
The plugin add some styles like overflow:hidden;
to cells and borders to the table and the cells (in order to keep on uniform border width I added: .MyTable {border-width:0 0 1px 1px;}
and .MyTable > tr > td {border-width:1px 1px 0 0 }
.
Now I am wonder how and where to put this css styles.
I can put them on external css file but then I force the user to add .css file to her page. In order to make the plugin accessable to all users, even the new ones, this could be an obstacle. A user might forget to add the css file or even change it's content in order to achieve something and this will make the plugin work unproperly.
In addition, I want to let the user the ability to change things (like the border's width). In case I put the css on external file, the user can change anything she desires by herself.
Another option, I can also assign the styles directly on the jquery elem (using elem.css(...) command). Thats way the user don't have to deal with css file in order to work properly and in the other way in order to allow changes in styles (like the border) I can add an option to the plugin. But, this way I creating unefficient code and I merge between behaviour and design.
Is there any other possibility to add styles to jquery plugins?
What is the best way to do it?
I am NOT talking about a style that should determine color or font, but styles that are necessary to the structure I make!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以动态地将
元素添加到页面。像这样的东西: http://jsfiddle.net/vpPpT/
上面的例子只是 这是您可以动态创建 DOM 元素的多种方法之一。
另外,这里有人试图做类似的事情(尽管出于所有错误的原因):
You can dynamically add a
<style>
element to the page. Something like this: http://jsfiddle.net/vpPpT/The above example is just one of many ways you can create DOM elements on-the-fly.
Also, here's someone that was trying to do something similar (although for all the wrong reasons):