javascript - 在 HTML5/js/PHP/CSS 中是否有一种标准的编程方式来创建可重用的对象?

标签 javascript php html css github

我(在空闲时间)创建了一个 html 页面,该页面使用 PHP 来创建我的动态对象。 此页面调用我的 javascript 以使用 AJAX 接收用户的输入。 我的对象使用 AJAX 进行输入(PHP 执行 $_GET),然后我的对象将打开到我的 PDO mySQL 数据库的连接,执行查询并接收详细信息。通过详细信息,它显示了我的对象并回显了 HTML5,我使用 CSS 实现了一些不错的功能。

它工作正常。我学到了很多东西(CSS 很棒),但与其他编程语言相比,这在学习上非常相似,但又非常不同! 我发现它的不同之处在于……对于我想做的任何事情,总是有许多不同的方法来做。由于这是一组一起运行的语言(PHP、javascript、CSS3 和 HTML5)。

我暂时以某种方式避免使用 jQuery,因为我正在学习并且更喜欢以困难的方式做所有事情。然而,我注意到很多这样的插件,例如条形图,都是用 javascript 创建的,并以数组的形式提供数据。

现在我想要第二个页面,它的功能与第一个页面相同,但访问不同的数据并可能执行不同的操作,但它的显示效果非常相似。 所以我希望能够再次使用我的对象,只是给它不同的参数、不同的输入等等……就好像我刚刚使用了一个插件一样。所以现在我正在考虑在 js 中实例化它并从那里输入数据。 我只是想要一些很好的指导,因为我刚开始,我学得越多,它就变得越广阔:)

所以这就是我的问题所在: 问:在 HTML5/js/PHP/CSS 中是否有一种标准的编程方式来创建可重用的对象? 欢迎提供引用资料和专业知识/经验,我不想展开辩论。我想要一个标准来遵循。

最佳答案

不清楚您到底在问什么,但这是我对我的理解的回答。

服务器端组织:

如果您要创建一个大元素,我强烈建议您使用 PHP 框架(symphonycodeigniter 等),如果您不这样做,请考虑以下事项:

<强>1。遵循MVC结构

MVC structure

MVC 结构可以用多种不同的方式表示。但是,为简单起见,您可以通过创建 3 个文件夹(controller、model、view)来完成此操作。

每个文件夹将包含具有特定作业的不同 php 文件。

  • Controller :

A controller is the link between a user and the system. It provides the user with input by arranging for relevant views to present themselves in appropriate places on the screen. It provides means for user output by presenting the user with menus or other means of giving commands and data. The controller receives such user output, translates it into the appropriate messages and pass these messages on to one or more of the views.

将控制者想象成警察。检查客户端请求是否合法(尤其是在表单验证方面)。当 Controller 验证请求时,它会调用模型(可选:取决于您是否需要获取/设置信息并使用数据库),然后是 View 。

  • 模型:

Models represent knowledge. A model could be a single object (rather uninteresting), or it could be some structure of objects.

There should be a one-to-one correspondence between the model and its parts on the one hand, and the represented world as perceived by the owner of the model on the other hand.

  • 查看:

A view is a (visual) representation of its model. It would ordinarily highlight certain attributes of the model and suppress others. It is thus acting as a presentation filter.

A view is attached to its model (or model part) and gets the data necessary for the presentation from the model by asking questions. It may also update the model by sending appropriate messages. All these questions and messages have to be in the terminology of the model, the view will therefore have to know the semantics of the attributes of the model it represents.

View 的有趣之处在于您可以将 HTML 页面分解为多个部分。主要思想是创建一个 header.php 和一个 footer.php,它们不会有太大变化(取决于您需要的 css/js)。然后您只需调用将填充 html 页面主体的 views

这很有用,因此当客户端请求主页时,您可以简单地执行以下操作:

include header.php;
include home.php;
include footer.php;

让我知道这是否有用。

如果您仍然不确定我在说什么,请随时阅读以下内容:

MVC tutorial 1

MVC tutorial 2

Youtube videos tutorial on how to create an MVC structure (basic)

Youtube videos tutorial on how to create an MVC structure (advanced)


Definition reference here

关于javascript - 在 HTML5/js/PHP/CSS 中是否有一种标准的编程方式来创建可重用的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29055824/

相关文章:

javascript - 如何将颜色值从一个 div 复制到另一个?

javascript - 如何重置在整个页面中应用的 jQuery 切换类? ( Angular 元素)

javascript - 如何将变量从index.html 文件加载到results.html 文件?

java - 解析 JSON 字符串返回未定义

php - 无法跨页面获取某些 session 变量

php - MySQL:基于另一个字段添加序列列

javascript - 到达页面底部后,如何让用户滚动到另一个特定页面?

JavaScript : Use of closure variable

javascript - 如何更改 jQuery 中动态创建的段落的背景颜色?

php - 使用 PHP 创建具有特定格式的 JSON