Jquery insertAfter() 似乎不适合我

发布于 2024-08-26 13:11:32 字数 2883 浏览 9 评论 0原文

我创建了一个父 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&nbsp;:</span>&nbsp;<span>' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" style="font-size:100%;font-weight:bolder;">Salary Basis&nbsp;:</span>&nbsp;<span>' + employee.SalaryBasis + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Salary&nbsp;:</span>&nbsp;<span>' + employee.FixedSalary + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Address&nbsp;:</span>&nbsp;<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&nbsp;:</span>&nbsp;<span>' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" style="font-size:100%;font-weight:bolder;">Salary Basis&nbsp;:</span>&nbsp;<span>' + employee.SalaryBasis + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Salary&nbsp;:</span>&nbsp;<span>' + employee.FixedSalary + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Address&nbsp;:</span>&nbsp;<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淑女气质 2024-09-02 13:11:32

反转循环:

for (var i = jsonObj.Table.length - 1; i >= 0; i--)

备注:不要使用eval。我建议您使用 JSON.parse 来代替,或者如果这个 JSON 来自 ajax 调用 jQuery 应该自动将其解析为对象。


更新:

要向这些 div 添加行颜色,您可以在循环之外尝试此操作:

$(".resultsdiv:odd").css("background-color", "#F4F4F8");
$(".resultsdiv:even").css("background-color", "#EFF1F1");

Reverse the loop:

for (var i = jsonObj.Table.length - 1; i >= 0; i--)

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:

$(".resultsdiv:odd").css("background-color", "#F4F4F8");
$(".resultsdiv:even").css("background-color", "#EFF1F1");
行至春深 2024-09-02 13:11:32

您要在父元素之后添加元素:

<div id="ResultsDiv">
</div>
<div class="resultsdiv">...4...</div>
<div class="resultsdiv">...3...</div>
<div class="resultsdiv">...2...</div>
<div class="resultsdiv">...1...</div>

但您可能想在父元素内部添加元素:

<div id="ResultsDiv">
  <div class="resultsdiv">...1...</div>
  <div class="resultsdiv">...2...</div>
  <div class="resultsdiv">...3...</div>
  <div class="resultsdiv">...4...</div>
</div>

使用appendTo方法而不是<代码>insertAfter 方法。

更新:

您添加了多个具有相同 id 的元素,这是不合法的。只需使用类来定位元素:

$(".resultsdiv:odd").css("background-color", "#F4F4F8");
$(".resultsdiv:even").css("background-color", "#EFF1F1");

You are adding elements after the parent element:

<div id="ResultsDiv">
</div>
<div class="resultsdiv">...4...</div>
<div class="resultsdiv">...3...</div>
<div class="resultsdiv">...2...</div>
<div class="resultsdiv">...1...</div>

but you probably want to add elements inside the parent element:

<div id="ResultsDiv">
  <div class="resultsdiv">...1...</div>
  <div class="resultsdiv">...2...</div>
  <div class="resultsdiv">...3...</div>
  <div class="resultsdiv">...4...</div>
</div>

Use the appendTo method instead of the insertAfter method.

Update:

You are adding several elements with the same id, which is not legal. Just use the class to target the elements:

$(".resultsdiv:odd").css("background-color", "#F4F4F8");
$(".resultsdiv:even").css("background-color", "#EFF1F1");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文