Conflicts between css style sheets and javascript

Source: Internet
Author: User

Problem:

When the background color of an element is set in the css style sheet, changing the background color with javascript will become invalid. How can you solve this conflict? If I want to use a style sheet.

The Code is as follows:

<Html> 

Tip: the code can be modified before running!

Answer:

Shouldn't the code "background = Red" appear in css code? It should be "background: Red", which is the first one; second, in the css Code, set "# tb td {border: 1px solid # EEE; padding: 4px; background: Red }", however, in JavaScript code, it is true to operate on its parent container tr. In this way, the color set for td must overwrite the tr color. Therefore, there are several ways to change it:

1. Set the background color of tr to red, and the td color remains unchanged:

Copy to ClipboardReference: [www.bkjia.com] <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html> <Title> </title>
<Style type = "text/css">
# Tb {width: 666px; border-collapse: collapse; border: 1px solid # EEE; font-size: 14px ;}
# Tb th {background: # EEE; border-bottom: 1px solid # CCC; padding: 4px ;}
# Tb tr {background: red ;}<! -- Set the background color in the tr tag -->
# Tb td {border: 1px solid # EEE; padding: 4px ;}
</Style> <Body>
<Table id = "tb">
<Tr> <th> item name </th> <th> unit price </th> <th> Inventory quantity </th> <th> goods location </th> </tr>
<Tr> <td> Ding Xue's favorite xiansi deluxe edition-lingsha edition </td> <td> 139 </td> <td> 10000000 </td> <td> a12-253 </td> </tr>
<Tr> <td> 139 </td> <td> 10000000 </td> <td> A12-254 </td> </tr>
<Tr> <td> xiansi general edition-starter </td> <td> 69 </td> <td> 10000000 </td> <td> A12-255 </td>/ tr>
</Table>
<Script type = "text/javascript">
Var obj = document. getElementById ("tb ");
// Var tds = obj. getElementsByTagName ("td ");
For (var I = 0; I <obj. rows. length; I ++ ){
Obj. rows [I]. onmouseover = function ()
{
This. style. background = "# 0ef ";
}
Obj. rows [I]. onmouseout = function ()
{
This. style. background = "red ";
}
}
</Script>
</Body> 2. Get td in js Code and change its color

Copy to ClipboardReference: [www.bkjia.com] <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html> <Title> </title>
<Style type = "text/css">
# Tb {width: 666px; border-collapse: collapse; border: 1px solid # EEE; font-size: 14px ;}
# Tb th {background: # EEE; border-bottom: 1px solid # CCC; padding: 4px ;}
# Tb td {border: 1px solid # EEE; padding: 4px; background: red ;}
</Style> <Body>
<Table id = "tb">
<Tr> <th> item name </th> <th> unit price </th> <th> Inventory quantity </th> <th> goods location </th> </tr>
<Tr> <td> Ding Xue's favorite xiansi deluxe edition-lingsha edition </td> <td> 139 </td> <td> 10000000 </td> <td> a12-253 </td> </tr>
<Tr> <td> 139 </td> <td> 10000000 </td> <td> A12-254 </td> </tr>
<Tr> <td> xiansi general edition-starter </td> <td> 69 </td> <td> 10000000 </td> <td> A12-255 </td>/ tr>
</Table>
<Script type = "text/javascript">
Var obj = document. getElementById ("tb ");
Var tds = obj. getElementsByTagName ("td"); // obtain each td and change its color.
For (var I = 0; I <tds. length; I ++ ){
Tds [I]. onmouseover = function ()
{
This. style. background = "# 0ef ";
}
Tds [I]. onmouseout = function ()
{
This. style. background = "red ";
}
}
</Script>
</Body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.