简体   繁体   English

jQuery将CSS应用于动态创建的表行

[英]Jquery Applying CSS to Dynamically created table rows

I have a HTML table, with rows being generated dynamically using Jquery. 我有一个HTML表,使用Jquery动态生成行。 The rows are created fine and the dynamically-generated Ids work perfectly. 行创建良好,并且动态生成的ID完美运行。 I want to adjust the width of a table cell that is generated every time. 我想调整每次生成的表格单元的宽度。

<table border='1' id='foresttable'>
                        <tr>
                            <td id='addforestcell'><img id='addforestimg' src='img/addsign.png' onclick="forestrotate()"></td>
                            <td></td>
                        </tr>
                        <tr>
                            <td>Forest Area #1</td>
                            <td><img src='img/cancel.png' href="#" title='Start Over' onclick="clearForest();" class='deletebtn'> Place a Forest/Group of Trees on the Map</td>
                        </tr>
                    </table>

Here is the JQuery: 这是JQuery:

 $(function(){
var tbl = $("#foresttable");

$("#addforestimg").click(function(){
    $("<tr><td>Forest Area #"+forestnum+"</td><td class='forestcol2'><button class='red mapbtn' onclick='changecolor(this);' id='forestbutton"+forestnum+"'> Place markers to define forested area #"+forestnum+"</button></td><td>Lorem Ipsum</td><td>Lorem Ipsum</td><td><button class='delRowBtn'>Delete</button></td></tr>").appendTo(tbl); 
    if(forestnum>1){
        $("#foresttable tr:nth-last-child(2)").html("<tr><td>Forest Area #"+(forestnum-1)+"</td><td class='forestcol2'><button class='red mapbtn' onclick='changecolor(this);' id='forestbutton"+(forestnum-1)+"'> Place markers to define forested area #"+(forestnum-1)+"</button></td><td>Lorem Ipsum</td><td>Lorem Ipsum</td><td><button class='delRowBtn'>Delete</button></td></tr>");
    }
    $(".forestcol2").css("width", 800);
forestnum++;
});      
$(document.body).delegate(".delRowBtn", "click", function(){
    $(this).closest("tr").remove(); 

});    

});

Every row created comes with a delete button, however I only want the last row to be deletable, so when a new row is created I alter the html of the second last row to remove it's delete button. 创建的每一行都带有删除按钮,但是我只希望最后一行是可删除的,因此在创建新行时,我更改了倒数第二行的html以删除它的删除按钮。

This all works fine, but I cannot change width of the second-last row whenever it is created. 一切正常,但是创建后我不能更改倒数第二行的宽度。 Well, I can, but only by using a series of ID selectors in CSS eg 好吧,我可以,但是只能通过在CSS中使用一系列ID选择器来实现,例如

#forestbutton1{}
#forestbutton2{}
etc...

I have tried applying a class to each row created (.forestcol2) as you can see above and it works when they're created, but not when I change the 2nd last element? 我已经尝试将一个类应用于所创建的每一行(.forestcol2),如您在上面看到的那样,它在创建它们时起作用,但是当我更改倒数第二个元素时不起作用吗? I have also tried: 我也尝试过:

$(".forestcol2").css("width", 800);

but no luck. 但没有运气。

Stumped here any help would be much appreciated. 困在这里任何帮助将不胜感激。

I noticed you wrote following code in your js. 我注意到您在js中编写了以下代码。

tr:nth-last-child(2)

It selected the second last element tr of your table. 它选择了表格的倒数第二个元素tr It is useful, right? 这很有用吧? You don't need to know what the id is here but only the position. 您不需要知道这里的ID,而只需知道位置。

And you know, css also supported similar features. 而且您知道,css还支持类似的功能。 For you requirements, it should like this. 根据您的要求,它应该像这样。

#foresttable tr:nth-last-child(2){
 // any properties you want here
}

More features please visit CSS3 :nth-last-child() Selector 更多功能请访问CSS3:nth-​​last-child()选择器

Many hours later, 很多小时后

It wasn't css or the JQuery, they were working perfectly. 不是css或JQuery,它们运行良好。 After using Chrome Developer Tools I could see they were being applied successfully, but something was overwriting them and setting width to 68px, another style or something. 使用Chrome开发人员工具后,我可以看到它们已成功应用,但是有一些东西覆盖了它们,并将宽度设置为68px,还有其他样式。

I spent about 4 hours digging through everything trying to find what was causing the problem but no luck. 我花了大约4个小时来仔细研究所有问题,以找出导致问题的原因,但是没有运气。

As the program is several thousand lines long, its not worth digging any further, I'm just going to use the Dynamic ID's. 由于程序长数千行,因此不值得进一步研究,我将使用动态ID。

But thanks to everyone whob answered/commented. 但是,感谢所有回答/评论的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM