简体   繁体   中英

How can I change CSS style, and focus on element as time goes by?

How can I change background color of <tr> with jquery?
Then how can I focus on <tr> element with jquery?

Javascript

$('tr#5').???????? to change background color of <tr#5> to #666666;
$('tr#5').???????? to focus on element "tr#1"; 

.
.
.
$('tr#5').???????? to change background color of <tr#5> back to #ffffff;
$('tr#10').???????? to change background color of <tr#10> to #666666;
$('tr#10').???????? to focus on element "tr#10"; 

HTML

<div style="border:solid 1px;height:70px; width:500px; overflow-y:scroll;">
    <table height="100" width="400">
    <tr id="5">
        <td>00:00:05</td>
        <td>hello! 5 secs has past</td>
    </tr>

    <tr id="10">
        <td>00:00:10</td>
        <td>hello! 10 secs has past</td>
    </tr>

    <tr id="10">
        <td>00:00:10</td>
        <td>hello! 10-2 secs has past</td>
    </tr>

    <tr id="11">
        <td>00:00:11</td>
        <td>hello! 11 secs has past</td>
    </tr>

    <tr id="30">
        <td>00:00:30</td>
        <td>hello! 30 secs has past</td>
    </tr>
    </table>
</div>

Firstly, it's invalid HTML to have non-unique element ids. In fact, before HTML5 it's also invalid to have all-numeric ids. They need to start with a letter followed by any number of letters, numbers, hyphen, colon or period.

That being said, what you're after is the following:

$('#5').css('background-color', '#666666');

I'm not sure what you you're trying to gain from attempting to focus on a non-interactive element, however, but an interactive element can be focused by using the .focus() method.

You could use timeouts like this:

setTimeout(function() { 
    $('tr#5').css('background', '#666666'); 
}, 5000);
setTimeout(function() { 
    $('tr#5').css('background', '#ffffff'); 
    $('tr#10').css('background', '#666666');
}, 10000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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