开发者

Changing the innerHTML of elements in a nested div tag

开发者 https://www.devze.com 2022-12-29 02:33 出处:网络
It has become apparent I\'m quite an idiot - there was an error in my JS above this code - sorry for the bother - all the below are correct in one form or another.. (also I\'m selecting the one that w

It has become apparent I'm quite an idiot - there was an error in my JS above this code - sorry for the bother - all the below are correct in one form or another.. (also I'm selecting the one that works in IE is the correct answer).. Thanks for all your help!!

-- Representative code only - I need to know how to change the innerhtml of the tablerow --

<html>
<head>
<script> //full javascript declaration - function call after page loads..
function ()
{
document.getElementById('tr1').innerHTML='<td>Test</td>'; // does not work..
document.getElementByTagNames('tr')[0].innerHTML='<td>Test</td>'; // does not work..
document.getElementById('div2').getElementsByTagNames('tr')[0].innerHTML='<td>Test</td>'; //does not work..
document.getElementById('div1').div2.tr1.innerHTML='<td>Test</td>'; //nope
}
</script>
</head>
<body>
<div id='div1'>
    <div id='div2'>
        <table>
            <tr id='tr1'></tr>
        </table>
    </div>
</div>
</body>
</html>

Thanks for the help

Let me clarify.. I have 400 lines of HTML/JS/Jquery code being dynamically generated so I'll simplify to the part that is a problem

This function is called when the body loads:

function loadinitialData()
{
$('#tabs').tabs();
document.getElementById('tr0').innerHTML = '<td>John Doe</td><td>3.988000</td><td>Candidate Doe</td>';
document.getElementById('tr1').innerHTML = '<td>Jane Doe</td><td>5.796000</td><td>Candidate Doe</td>';
}

This is in the body:

<div id="tabs" class="ui-corner-top"> 
            <ul> 
                <li><a href="#tabs-1">Some Idiots</a></li> 
                <li><a href="#tabs-2">Other Idiots</a></li> 
            </ul>
            <div id="tabs-1">
            <table width="59开发者_开发知识库0px" cellpadding="4px" rules="groups" frame="void" bordercolor="white">
            <tr><td>Name</td><td>Time Taken</td><td>Something</td></tr>
            <tbody>
            <tr style='font-size:12px' id='tr0'></tr><tr style='font-size:12px' id='tr1'></tr></tbody></table></div>
            <div id="tabs-2">Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum.</div> 
</div>

PS: Using jquery tabs but that can't be a problem..

Per @Gero I tried the following: Changed all the loading to:

document.getElementById('tablet10').rows[0].cells[0].innerHTML = 'John Doe';
document.getElementById('tablet10').rows[0].cells[1].innerHTML ='3.988000';
document.getElementById('tablet10').rows[0].cells[2].innerHTML ='Candidate Doe';

Blah Blah - all the rest..

In the body

<div id="tabs-1"> 
            <table id="tablet10" width="590px" cellpadding="4px" rules="groups" frame="void" bordercolor="white"> 
            <tr><td>Name</td><td>Time Taken</td><td>Something</td></tr> 
            <tbody> 
            <tr style='font-size:12px'><td></td><td></td><td></td></tr><tr style='font-size:12px'><td></td><td></td><td></td></tr></tbody></table></div> 

Per @plodder loading:

document.getElementById('tr0').insertCell(0).innerHTML = 'John Doe';
document.getElementById('tr0').insertCell(1).innerHTML ='3.988000';
document.getElementById('tr0').insertCell(2).innerHTML ='Candidate Doe';

and the rest blah blah..

and body:

<div id="tabs-1"> 
            <table width="590px" cellpadding="4px" rules="groups" frame="void" bordercolor="white"> 
            <tr><td>Name</td><td>Time Taken</td><td>Something</td></tr> 
            <tbody> 
            <tr style='font-size:12px' id='tr0'></tr><tr style='font-size:12px' id='tr1'></tr></tbody></table></div> 


The code will fail in IE because TR.innerHTML is not writeable in IE

You should use the DOM API for table rows

document.getElementById('tr1').insertCell(myCell);


Here you can see an example of working code. http://www.w3schools.com/js/tryit.asp?filename=try_dom_tablerow_cells


"The property is read/write for all objects except the following, for which it is read-only: COL, COLGROUP, FRAMESET, HEAD, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR."

"DHTML expressions can be used in place of the preceding value(s)."

http://msdn.microsoft.com/en-us/library/ms533897.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消