Jquery insertAfter() 似乎不适合我
我创建了一个父 div 并使用 jquery insertAfter()
方法在父 div 之后插入了 div...发生的情况是我的第一条记录转到底部,下一条记录插入到它上面... 。
这是我的功能...
function Iteratejsondata(HfJsonValue) {
var jsonObj = eval('(' + HfJsonValue + ')');
for (var i = 0, len = jsonObj.Table.length; i < len; ++i) {
var employee = jsonObj.Table[i];
$('<div class="resultsdiv"><br /><span id="EmployeeName" style="font-size:125%;font-weight:bolder;">' + employee.Emp_Name + '</span><span style="font-size:100%;font-weight:bolder;padding-left:100px;">Category :</span> <span>' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" style="font-size:100%;font-weight:bolder;">Salary Basis :</span> <span>' + employee.SalaryBasis + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Salary :</span> <span>' + employee.FixedSalary + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Address :</span> <span>' + employee.Address + '</span></div>').insertAfter('#ResultsDiv');
}
}
我的结果是,
alt text http://img265 .imageshack.us/img265/7646/divresult.jpg
Palani 必须在我的父 div 旁边,但它在底部......因为 insertAfter()
在 #ResultsDiv
旁边插入每条记录...任何建议如何在新生成的 div 之后插入...
编辑: 如何向我使用的这些 div 添加行颜色,
function Iteratejsondata(HfJsonValue) {
var jsonObj = JSON.parse(HfJsonValue);
for (var i = jsonObj.Table.length - 1; i >= 0; i--) {
var employee = jsonObj.Table[i];
$('<div id="resDiv" class="resultsdiv"><br /><span id="EmployeeName" style="font-size:125%;font-weight:bolder;">' + employee.Emp_Name + '</span><span style="font-size:100%;font-weight:bolder;padding-left:100px;">Category :</span> <span>' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" style="font-size:100%;font-weight:bolder;">Salary Basis :</span> <span>' + employee.SalaryBasis + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Salary :</span> <span>' + employee.FixedSalary + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Address :</span> <span>' + employee.Address + '</span></div>').insertAfter('#ResultsDiv');
}
$("#resDiv.resultsdiv:odd").css("background-color", "#F4F4F8");
$("#resDiv.resultsdiv:even").css("background-color", "#EFF1F1");
}
但没有用。
I have created a parent div and inserted div's after the parent div using jquery insertAfter()
method.... What happens is My first record goes to the bottom and next record gets inserted above it....
Here is my function...
function Iteratejsondata(HfJsonValue) {
var jsonObj = eval('(' + HfJsonValue + ')');
for (var i = 0, len = jsonObj.Table.length; i < len; ++i) {
var employee = jsonObj.Table[i];
$('<div class="resultsdiv"><br /><span id="EmployeeName" style="font-size:125%;font-weight:bolder;">' + employee.Emp_Name + '</span><span style="font-size:100%;font-weight:bolder;padding-left:100px;">Category :</span> <span>' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" style="font-size:100%;font-weight:bolder;">Salary Basis :</span> <span>' + employee.SalaryBasis + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Salary :</span> <span>' + employee.FixedSalary + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Address :</span> <span>' + employee.Address + '</span></div>').insertAfter('#ResultsDiv');
}
}
And my result is,
alt text http://img265.imageshack.us/img265/7646/divresult.jpg
Palani must be next to my parent div but it is at the bottom... Because insertAfter()
inserts every record next to the #ResultsDiv
... Any suggestion how to insertafter the newly generated div...
EDIT:
how to add row color to these divs i used
function Iteratejsondata(HfJsonValue) {
var jsonObj = JSON.parse(HfJsonValue);
for (var i = jsonObj.Table.length - 1; i >= 0; i--) {
var employee = jsonObj.Table[i];
$('<div id="resDiv" class="resultsdiv"><br /><span id="EmployeeName" style="font-size:125%;font-weight:bolder;">' + employee.Emp_Name + '</span><span style="font-size:100%;font-weight:bolder;padding-left:100px;">Category :</span> <span>' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" style="font-size:100%;font-weight:bolder;">Salary Basis :</span> <span>' + employee.SalaryBasis + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Salary :</span> <span>' + employee.FixedSalary + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Address :</span> <span>' + employee.Address + '</span></div>').insertAfter('#ResultsDiv');
}
$("#resDiv.resultsdiv:odd").css("background-color", "#F4F4F8");
$("#resDiv.resultsdiv:even").css("background-color", "#EFF1F1");
}
But didnt work..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
反转循环:
备注:不要使用
eval
。我建议您使用 JSON.parse 来代替,或者如果这个 JSON 来自 ajax 调用 jQuery 应该自动将其解析为对象。更新:
要向这些 div 添加行颜色,您可以在循环之外尝试此操作:
Reverse the loop:
Remark: don't use
eval
. I would recommend you JSON.parse instead or if this JSON is coming from an ajax call jQuery should automatically parse it to object.UPDATE:
To add row color to these divs you could try this outside the loop:
您要在父元素之后添加元素:
但您可能想在父元素内部添加元素:
使用
appendTo
方法而不是<代码>insertAfter 方法。更新:
您添加了多个具有相同 id 的元素,这是不合法的。只需使用类来定位元素:
You are adding elements after the parent element:
but you probably want to add elements inside the parent element:
Use the
appendTo
method instead of theinsertAfter
method.Update:
You are adding several elements with the same id, which is not legal. Just use the class to target the elements: