dqnf28092 2015-04-29 09:00
浏览 25
已采纳

将html代码返回给jquery是否合适?

I'm working on a project where it has a number of pages. Each page displays 10 rows where the layout that is using for each page is different. Until now I had the html code of each row in a javascript code and based on the page's url I was using the appropriate html code (if statement). The if statement is inside into a loop which is looping based on the number of rows. The results of the rows are coming from an ajax method. Now I want somehow to separate it so it can be more easily for me to maintain it, basically to remove the html code from the javascript and keep each row's html code into a different file.

Note: the Ajax is in a given time, is sending automatically requests to the php file for any new rows.

One solution which I came out is that I can use the php to create a variable with the html code .

Second solution is to create an array of each record with the html code and then pass it to jquery to print it.

Both solutions I don't know if are good solutions and can help me to maintain the project in the future.

  • 写回答

3条回答 默认 最新

  • douqian5920 2015-04-29 09:39
    关注

    You might consider a template library such as handlebars to help with templating. Frameworks such as AngularJS and Ember also excel at solving these kinds of problems.

    Your Web Services API should be returning JSON though, not HTML fragments. Let the client build the DOM, and let the server focus on data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题