gpt4 book ai didi

javascript - 如何为带有 td 和输入标签的动态 tr 应用 css 样式?

转载 作者:行者123 更新时间:2023-11-27 23:39:03 25 4
gpt4 key购买 nike

我呈现动态附加 tr , td 和带有类名的输入标签,我还为单独的 css 编写样式,但它未应用动态 tr

请找到我的代码片段。

 <table style="width:100%" class="tSheet">
<tr class="t-header">
<th class="t-head">sno</th>
<th class="t-head">Description</th>
<th class="t-head">Debit</th>
<th class="t-head">Credit</th>
</tr>
<tr>
<td class="t-content"><input type="text" class="t-input" /></td>
<td class="t-content"><input type="text" class="t-input" /></td>
<td class="t-content"><input type="text" class="t-input" /></td>
<td class="t-content"><input type="text" class="t-input" /></td>
</tr>
</table>

CSS 样式:

.tSheet .t-head,.t-content  {
border: 1px solid black;
}
.t-input{
border:0px none;
}
.t-input:focus{
outline:0;
}

我的 javaScript 函数:

 $(function () {

$('input').css("width", $('input').parent().width());
for(var i=0;i<3;i++)
{
$("<tr><td class='t-content'><input class='t-content t-input/>'</td><td class='t-content'><input class='t-input/>'</td><td class='t-content'><input class='t-input/>'</td><td class='t-content'><input class='t-input/>'</td></tr>").attr({ class: "a" }).appendTo('.tSheet')
}
})

最佳答案

这是因为 class属性关闭错位

  1. 而不是使用 attr()使用 addClass()用于添加类
  2. 要应用宽度,请使用 css()使用回调,也可以在添加动态元素后执行,或者您可以设置 css width:100%输入元素
  3. 在你的代码中类名属性没有正确关闭,更改<input class='t-content t-input/>'<input class='t-content t-input'/><input class='t-input/>'<input class='t-input'/>

for (var i = 0; i < 3; i++) {
$("<tr><td class='t-content'><input class='t-content t-input'/></td><td class='t-content'><input class='t-input'/></td><td class='t-content'><input class='t-input'/></td><td class='t-content'><input class='t-input'/></td></tr>").addClass("a").appendTo('.tSheet')
}
$('input').css("width", function() {
return $(this).parent().width()
});
.tSheet .t-head,
.t-content {
border: 1px solid black;
}
.t-input {
border: 0px none;
}
.t-input:focus {
outline: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table style="width:100%" class="tSheet">
<tr class="t-header">
<th class="t-head">sno</th>
<th class="t-head">Description</th>
<th class="t-head">Debit</th>
<th class="t-head">Credit</th>
</tr>
<tr>
<td class="t-content">
<input type="text" class="t-input" />
</td>
<td class="t-content">
<input type="text" class="t-input" />
</td>
<td class="t-content">
<input type="text" class="t-input" />
</td>
<td class="t-content">
<input type="text" class="t-input" />
</td>
</tr>
</table>

关于javascript - 如何为带有 td 和输入标签的动态 tr 应用 css 样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32557812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com