简体   繁体   English

在foreach生成的html表中选择单元格

[英]Select cell in html table generated by foreach

please help me with knockout.js code 请帮我提供基因敲除代码

I try select element in table by id and change it's css style, but all rows have same id and I can't using function getElementById. 我尝试通过id选择表中的元素并更改其css样式,但是所有行都具有相同的id,并且我无法使用函数getElementById。 How I can do this simple thing ? 我该怎么做这个简单的事情?

<tbody data-bind="foreach: times">
        <tr>
            <td id=$index() data-bind="click: $root.select.bind($data, $index(), 0)> </td>

        ....

        <td id=$index() data-bind="click: $root.select.bind($data, $index(), 19)> </td>

    <tr>
</tbody>

Id's should always be unique. ID应该始终是唯一的。 Assign the same class to all elements you're interested in and use a bit of jquery: 将相同的类分配给您感兴趣的所有元素,并使用一些jquery:

document.getElementsByClassName('class_name')

EDIT: Good point. 编辑:好点。 I was originally going to suggest using jquery and then remembered this function. 我本来打算建议使用jquery,然后想起了这个功能。 If you are using jquery library, you can also try this: 如果您使用的是jQuery库,则也可以尝试以下操作:

$('.class_name').each(function(index) {
    ...do something...
});

EDIT: to answer your question, there are a few ways to do this: 编辑:要回答您的问题,有几种方法可以做到这一点:

$('.class_name').attr('id', new_id)

or 要么

$('.class_name').addClass('class_name')

depending on what exactly you're trying to do 取决于您到底要做什么

Try to use such code: 尝试使用以下代码:

<tbody data-bind="foreach: times">
    <tr>
        <td data-bind="attr: {id: $index()}, click: $root.select.bind($data, $index(), 0)></td>
    <tr>
</tbody>

Read more about attr binding here: http://knockoutjs.com/documentation/attr-binding.html 在此处阅读有关attr绑定的更多信息: http : //knockoutjs.com/documentation/attr-binding.html

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

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