Vous êtes sur la page 1sur 2

Veba 2

Rad sa javascript objektima

1. Korienje new Object


<body>
<script>
var osoba = new Object();
osoba.ime = "Pera";
osoba.prezime = "Peric";
osoba.starost = 25;
osoba.telefon = "123456";

document.write("<strong>" + osoba.ime + " " + osoba.prezime + " " + osoba.starost


+ osoba.telefon + "</strong>");
</script>
</body>

2. Rad sa javascript konstruktorom


<script>
function Osoba(ime, prezime, starost, telefon) {
this.ime = ime;
this.prezime = prezime;
this.starost = starost;
this.telefon = telefon;
}
var os1 = new Osoba("Pera", "Peric", 35, "123456");
var os2 = new Osoba("Mika", "Mikic", 38, "234567");
var os3 = new Osoba("Laza", "Lazic", 33, "934567");
var osobe = [os1, os2, os3];
var rezultat =
"<table><tr><th>Ime</th><th>Prezime</th><th>Starost</th><th>Telefon</th></tr>";
for (var i in osobe) {
rezultat += "<tr>";
rezultat += "<td>" +
rezultat += "<td>" +
rezultat += "<td>" +
rezultat += "<td>" +
rezultat += "</tr>";
}
rezultat += "</table>";
document.write(rezultat);
</script>

osobe[i].ime + "</td>";
osobe[i].prezime + "</td>";
osobe[i].starost + "</td>";
osobe[i].telefon + "</td>";

Stilizovanje tabele
<style type="text/css">
table {
border-collapse: collapse;
border: 2px solid black;
}
th, td {
border: 1px solid black;
padding: 5px;
}
th {
background-color: blue;
color: white;
}
td {
background-color: lightblue;
}
</style>

Vous aimerez peut-être aussi