简体   繁体   English

如何更改边框的颜色 <td> 使用JavaScript?

[英]How can I change the border colour of a <td> using JavaScript?

I'm trying to change the colour of a <td> in my .html file. 我正在尝试更改.html文件中<td>的颜色。 Here is what I have but for some reason it doesn't seem to be working. 这是我所拥有的,但是由于某种原因,它似乎无法正常工作。

My .html file: 我的.html文件:

<table>
    <tr id = "table_row">
        <td>Computers</td>
        <td>Price</td>
        <td>Location</td>
   </tr>
</table>

And now my .js file: 现在我的.js文件:

function changeBorderColor() {
    var table = document.getElementById("table_row").getElementsByTagName("td");
    table.style.borderColor = "red";
}

Why doesn't my <td> change colour when I activate the changeBorderColor() function? 为什么在激活changeBorderColor()函数时<td>不会更改颜色? Thanks in advance for any tips and help! 在此先感谢您的提示和帮助!

The getElementsByTagName() method will return an array of elements matching the tag selector. getElementsByTagName()方法将返回与标签选择器匹配的元素数组。 So you would need to iterate througth it to change each element: 因此,您需要遍历它以更改每个元素:

Simple example: 简单的例子:

function changeBorderColor() {
    var table = document.getElementById("table_row").getElementsByTagName("td");
    for(var i=0; i<table.length; i++) {
        var td = table[i];
        td.style.borderColor = "red";
    }
}

.getElementsByTagName("td") returns a NodeList, you will have to loop through each td and assign the border individually. .getElementsByTagName("td")返回一个NodeList,您将必须遍历每个td并分别分配border

You also need to specify the borderWidth and borderStyle properties. 您还需要指定borderWidthborderStyle属性。

 function changeBorderColor() { var td = document.getElementById("table_row").getElementsByTagName("td"); for (i = 0; i < td.length; i++) { td[i].style.borderColor = "red"; td[i].style.borderWidth = "1px"; td[i].style.borderStyle = "solid"; } } changeBorderColor() 
 <table> <tr id="table_row"> <td>Computers</td> <td>Price</td> <td>Location</td> </tr> </table> 

You can use some Jquery here.For example: 您可以在此处使用一些Jquery,例如:

function changeBackground(){

$("td").css("border","2px solid red");

}

If you are allowed to use jQuery, here is a slightly more verbose example: 如果允许使用jQuery,则下面是一个更详细的示例:

$('#table_row td').each(function(index){

   //Do something with each result. In this case - add a border
   $(this).css({ 'border': '1px solid red' });

});

Here is a fiddle for the jQuery example: http://jsfiddle.net/k3d6peLy/ 这是jQuery示例的小提琴: http : //jsfiddle.net/k3d6peLy/

Alternatively, if you don't have to worry about supporting anything before IE8, you could also use JavaScript's 'querySelectorAll()' method: 另外,如果您不必担心在IE8之前支持任何功能,则还可以使用JavaScript的'querySelectorAll()'方法:

var elements = document.querySelectorAll('#table_row td');

for(var i = 0; i < elements.length; i++){
    elements[i].style.border = '1px solid red';
}

Here is a fiddle for the 'querySelectorAll()' example: http://jsfiddle.net/k3d6peLy/1/ 这是'querySelectorAll()'示例的小提琴: http : //jsfiddle.net/k3d6peLy/1/

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

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