Witam
Postanowiłem zrobić sobie małą aplikację w JS do wpisywania gitarowych tabulatur. Wszystko działa ładnie, ale nie pod IE (
westchnienie). Żeby było śmieszniej nie wyrzuca mi żadnych błędów - po prostu nie widać tabelki, która powinna się załadować. Oto kod:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl"> <head><!-- ščřžýŠČŘŽÝ --> <meta name="generator" content="PSPad editor, www.pspad.com" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" />
A teraz kodzik JS:
var background = new Array('line.jpg','#ffffff');
var note = new Array('30px','50px');
var space = new Array('40px','40px');
var max_frets = 24;
var strings = 6;
var element;
function Initialize(id)
{
element = document.getElementById(id);
var table = document.createElement('table');
table.setAttribute('cellspacing','0');
table.setAttribute('cellpadding','0');
table.setAttribute('border','0');
for (var n=0;n<strings+2;n++)
table.appendChild(document.createElement('tr'));
element.appendChild(table);
AddNode();
}
function AddNode()
{
var table = element.getElementsByTagName('table')[element.getElementsByTagName('table').length-1];
var tr = table.getElementsByTagName('tr');
for (var n=0;n<tr.length;n++)
{
var td = document.createElement('td');
if (n==0)
td.style.height = space[0];
else if (n==tr.length-1)
td.style.height = space[1];
else
{
td.style.height = note[0];
td.style.backgroundImage = 'url(''+background[0]+'\')';
td.style.backgroundRepeat = 'repeat-x';
td.style.backgroundPosition = 'center left';
td.setAttribute('onclick','SelectFret(this);');
}
td.backgroundColor = background[1];
td.style.width = note[1];
td.style.textAlign = 'center';
td.setAttribute('valign','center');
td.innerHTML = ' ';
tr[n].appendChild(td);
}
}
function SelectFret(td)
{
if (td.getElementsByTagName('select').length > 0) return;
var select = document.createElement('select');
var value = td.innerHTML;
td.innerHTML = '';
var option = document.createElement('option');
option.text = '';
option.value = '';
if (option.value==value)
option.setAttribute('selected','selected');
select.appendChild(option);
for (var k=1;k<=max_frets;k++)
{
var option = document.createElement('option');
option.text = k+'';
option.value = k+'';
if (option.value==value)
option.setAttribute('selected','selected');
select.appendChild(option);
}
select.setAttribute('onblur','UnselectFret(this.parentNode);');
td.appendChild(select);
}
function UnselectFret(td)
{
if (td.getElementsByTagName('select').length == 0) return;
var select = td.getElementsByTagName('select')[0];
var value = select.options[select.selectedIndex].value;
td.removeChild(select);
td.innerHTML = value;
if (td == td.parentNode.lastChild && value != '')
AddNode();
}
Dużo tego, więc omówię pokrótce:
Te 5 zmiennych na górze, to taka mała konfiguracja. Skrypt ładuje się przy pomocy funcji
Initialize, która tworzy tabelkę, przypisuje jej odpowiednią ilość wierszy i przypisuje tabelkę do odpowiedniego elementu nadrzędnego. Funkcja
AddNode dodaje nowe komórki do tabeli (tyle, ile jest wierszy). Pozostałe dwie funkcje służą do podawania odpowiednich wartości -
SelectFret tworzy w komórce listę (wywoływana przy kliknięciu na komórkę), a
UnselectFret usuwa listę wpisując wartość do komórki (wywołana przy zdarzeniu onblur na utworzonym wcześniej select-ie).
Czy ktoś mi może powiedzieć, czemu IE tego nie łyka?
EDITNo to udało mi się.

Poprawiłem kod JS. W skrócie - IE ma problem z, nazwijmy to, "wprowadzaniem w życie" atrybutów ustalonych za pomocą metody "
setAttribute" - trzeba było to pozamieniać na przypisywanie właściwościom odpowiednich wartości. Poza tym nie udało mi się (w IE) dodać do tabelki nowego wiersza, nowej komórki do wiersza tabeli i nowej opcji do selecta metodą "
appendChild" - musiałem skorzystać z "
insertRow" i "
insertCell" i "
add". A cały, poprawiony, kod wygląda tak:
var background = new Array('line.jpg','#ffffff');
var note = new Array('30px','50px');
var space = new Array('40px','40px');
var max_frets = 24;
var strings = 6;
var begin = '1px solid #000000';
var element;
function Initialize(id)
{
element = document.getElementById(id);
var table = document.createElement('table');
table.cellSpacing = 0;
table.cellPadding= 0;
table.border = 0;
for (var n=0;n<strings+2;n++)
table.insertRow(n);
element.appendChild(table);
AddNode();
}
function AddNode()
{
var table = element.getElementsByTagName('table')[element.getElementsByTagName('table').length-1];
var tr = table.rows;
for (var n=0;n<tr.length;n++)
{
var td = tr[n].insertCell(tr[n].cells.length);
if (n==0)
td.style.height = space[0];
else if (n==tr.length-1)
td.style.height = space[1];
else
{
td.style.height = note[0];
td.style.backgroundImage = 'url(''+background[0]+'\')';
td.style.backgroundRepeat = 'repeat-x';
td.style.backgroundPosition = 'center left';
td.style.cursor = 'pointer';
td.onclick = function(){SelectFret(this);};
if (tr[n].cells.length == 1)
td.style.borderLeft = begin;
}
td.style.backgroundColor = background[1];
td.style.width = note[1];
td.style.textAlign = 'center';
td.vAlign = 'center';
td.innerHTML = ' ';
}
}
function SelectFret(td)
{
if (td.getElementsByTagName('select').length > 0) return;
var select = document.createElement('select');
var value = td.innerHTML;
td.innerHTML = '';
var option = document.createElement('option');
option.text = '';
option.value = '';
if (option.value == value)
option.selected = true;
try
{ select.add(option,null); }
catch(ex)
{ select.add(option); }
for (var k=0;k<=max_frets;k++)
{
var option = document.createElement('option');
option.text = k+'';
option.value = k+'';
if (option.value==value)
option.selected = true;
try
{ select.add(option,null); }
catch(ex)
{ select.add(option); }
}
select.onblur = function(){ UnselectFret(this.parentNode); };
select.focus();
td.appendChild(select);
}
function UnselectFret(td)
{
if (td.getElementsByTagName('select').length == 0) return;
var select = td.getElementsByTagName('select')[0];
var value = select.options[select.selectedIndex].value;
td.removeChild(select);
td.innerHTML = value;
if (td == td.parentNode.lastChild && value != '')
AddNode();
}
Kliknął bym sam sobie "pomógł", ale to by było małe przegięcie.
Ten post edytował qqrq 26.03.2008, 14:15:17