- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我读到这个page附加很多元素是不好的做法,我应该在循环的每次迭代期间构建一个字符串,然后将 DOM 元素的 HTML 设置为该字符串。在循环中使用过多的 HTML 是否也是如此?
我有一个解析 JSON 数据的 AJAX 脚本。它需要向不同的现有元素添加数据,如下所示:
$.ajax({
url: "url",
success: function (data) {
$(data.query.results.json.json).each(function (index, item) {
var title = item.title, // A,B,C or D
age = item.age,
background = item.background,
ingredient = item.Ingredient;
$('.'+ title+'_ingredient').html(''+ingredient+'')
$('.'+ title+'_age').html(''+age+'')
$('.'+ title+'_background').html(''+background+'')
});
},
error: function () {}
});
HTML:
<div class="A_ingredient"></div>
<div class="B_ingredient"></div>
<div class="C_ingredient"></div>
<div class="D_ingredient"></div>
<div class="A_age"></div>
<div class="B_age"></div>
<div class="C_age"></div>
<div class="D_age"></div>
<div class="A_background"></div>
<div class="B_background"></div>
<div class="C_background"></div>
<div class="D_background"></div>
是否需要先建立一个字符串?如果是这样,你能告诉我怎么做吗?
最佳答案
这纯粹是关于处理对 html() 的调用所花费的时间,因此他们只是建议您减少调用次数。在这种情况下,您可以在循环中构建它们一次,然后为每个设置一次 div html。
根据您的更新,除了您不需要添加的所有额外尾随引号(字符串是字符串是字符串)之外,您的代码没有问题。 您只能击中每个项目一次。
例如
$.ajax({
url: "url",
success: function (data) {
$(data.query.results.json.json).each(function (index, item) {
var title = item.title, // A,B,C or D
age = item.age,
background = item.background,
ingredient = item.Ingredient;
$('.'+ title+'_ingredient').html(ingredient);
$('.'+ title+'_age').html(age);
$('.'+ title+'_background').html(background);
});
},
error: function () {}
});
注意:如果您的项目属性(Age、Background、Ingredient
)是简单值(不是对象或数组),您不需要前导 ''+
要么。
假设你真的想连接结果(你现在只保留最后一个成分),你可以这样做:
$.ajax({
url: "url",
success: function (data) {
var ingredients = '';
$(data.query.results.json.json).each(function (index, item) {
var title = item.title;
var ingredient = item.Ingredient;
ingredients += ingredient;
});
$('.aclass').html(ingredients);
$('.bclass').html(ingredients);
$('.cclass').html(ingredients);
$('.dclass').html(ingredients);
},
error: function () {}
});
可以简化为:
$('.aclass,.bclass,.cclass,.dclass').html(ingredients);
您的示例中每个 div 的内容都相同,因此您只需要一个字符串。
在这种情况下,您可能需要在成分之间使用某种形式的分隔符,但您的示例太模糊了。
例如
ingredients += ingredient + '<br/>';
关于javascript - ajax 脚本中的 HTML 过多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23949997/
我在 JBoss 6 上使用 JSF 2。我制作了一个示例应用程序,它使用 JSF 作为 View ,使用 EJB 作为逻辑,使用 JPA 作为持久性。 JSF 中的 bean 具有 RequestS
我打开气流并检查了连接,然后发现背后有太多连接。 关于如何杀死那些我不使用的想法,或者我很想知道运行它的最低conn_id的任何想法。 建筑 LocalExecutor(与其他经纪人不同) Postg
我收到错误 style.css:1 GET https://www.example.com/cs_CZ净::ERR_TOO_MANY_REDIRECTS 可能与我的 htaccess 有关吗? Rew
我收到错误 style.css:1 GET https://www.example.com/cs_CZ净::ERR_TOO_MANY_REDIRECTS 可能与我的 htaccess 有关吗? Rew
我正在 Android 中开发一个应用程序,我有以下 XML 布局:
我有 5 个类用作运算符: TurnOn , TurnOff , PushBox , Exit , Enter 对于每种类型,我都有一个包含该类型描述的字符串。 例如: class Places {
我们在使用 lettuce redis 库时遇到了这个具体问题。我们收到太多 RedisCommandTimeoutException。我们在 redis-cli 中设置了 2 秒的超时,在 redi
在 Controller 类文件中,大多数方法函数都包含try/catch块,如下所示: try { $stmt = $this->prepare($sql); $st
我读到这个page附加很多元素是不好的做法,我应该在循环的每次迭代期间构建一个字符串,然后将 DOM 元素的 HTML 设置为该字符串。在循环中使用过多的 HTML 是否也是如此? 我有一个解析 JS
在我的应用程序日志上运行 logcat 时,我看到很多 GC_FOR_ALLOC 而没有 GC_CONCURRENT 消息。我知道我有内存泄漏,但在堆栈和其他站点的任何地方,logcat 消息都有很多
当我检查我的 Tomcat 应用程序管理器时,我发现我的 session 太多(超过 4600): 为什么会这样?这可以链接到已部署的应用程序,还是它已经运行了很长时间? 最佳答案 这可能有多种原因,
所以我试图在一个 div 中运行 3 个 float ,但我遇到了这个问题。 http://jsfiddle.net/sPUjQ/101/ 点我 每当我运行两次 float 时,背景颜色会保持不变,但
为什么在使用 http_build_query 函数构建查询字符串时,它会将方括号 [] urlencode 到值之外,如何摆脱它? $query = array("var" => array("fo
在一个过程中,我需要使用许多 if-else 条件来决定列的值。该脚本以 FOR rec IN (SELECT....) LOOP Begin 并决定许多不同的值 rec 子记录可以在每次迭代中获得。
我们基于 Java 的服务器应用程序报告异常长的类加载时间: # jstat -class 10625 1000 Loaded Bytes Unloaded Bytes Time 4
我正在使用 Kraken.js 开发一个应用程序并管理我决定使用的 session :connect-mongo。 我有这样的设置: 'use strict'; var session = requi
我在 iPad 应用程序中有 180 个 UILabels(UITableViewCells 的 subview ),每个 UILabel 的宽度为 155 X 155,并且每个包含大量 Unicod
所以基本上,如果我将 JPanel 放入使用 GridBagLayout 的 JPanel 中,并使用 setPreferredSize 限制大小>,最终它达到了无法容纳所有这些的地步,并且它表现出附
我目前使用的是 Play 2.2.1,在白天运行生产 Web 应用程序后,我的日志中开始出现以下错误。似乎我的连接已用完/我一定没有正确关闭某些东西,但我似乎无法弄清楚它是什么。 2014-02-11
所以我几乎完成了我的元素,我打算创建一个完整的网页 slider 作为我网站的背景主题,并且它运行完美! 问题是它的垂直拉伸(stretch)太长,您可以向右滚动(您不应该那样做)而且我不知道为什么它
我是一名优秀的程序员,十分优秀!