Вход

Просмотр полной версии : при отсутствии елемента код не работает


новый вася
03.08.2016, 21:35
Почему оно работает только если все елементы isset ? если только какого-нибуть елемента который getElementById нету то все ппц не пашет...

function sortir() {
var a = document.getElementById('id2O').style.top;
var b = document.getElementById('id2D').style.top;
var c = document.getElementById('id2S').style.top;

if (a != '5px') {
document.getElementById('id2O').style.top = "5px";
} else {
document.getElementById('id2O').style.top = "44px";
}


if (c != '5px') {
document.getElementById('id2S').style.top = "5px";
} else {
document.getElementById('id2S').style.top = "64px";
}


if (b != '5px') {
document.getElementById('id2D').style.top = "5px";
} else {
document.getElementById('id2D').style.top = "84px";
}
return false; }

три линии находятся друг под другом по клику задаеться топ 5 по второму снова дефолт..Когда они все три линии есть все ок, как только какойто нету, не пашет =(

рони
03.08.2016, 21:59
новый вася,
что мешает проверить вначале есть линия или нет, если есть тогда работать со стилем.

новый вася
03.08.2016, 22:06
if (variable !== undefined) или есть какойто более приемлимый аналог PHPшного isset() ?

рони
03.08.2016, 22:12
новый вася,
function sortir() {
var a = document.getElementById("id2O");
var b = document.getElementById("id2D");
var c = document.getElementById("id2S");
if (a)
if (a.style.top != "5px") a.style.top = "5px";
else a.style.top = "44px";
if (b)
if (b.style.top != "5px") b.style.top = "5px";
else b.style.top = "64px";
if (c)
if (c.style.top != "5px") c.style.top = "5px";
else c.style.top = "84px";
return false
};

новый вася
03.08.2016, 22:13
if(document.getElementById('id2O') !== 'undefined') {
if (a != '5px') {
document.getElementById('id2O').style.top = "5px";
} else {
document.getElementById('id2O').style.top = "44px";
}}

if(document.getElementById('id2S') !== 'undefined') {
if (c != '5px') {
document.getElementById('id2S').style.top = "5px";
} else {
document.getElementById('id2S').style.top = "64px";
}}

if(document.getElementById('id2D') !== 'undefined') {
if (b != '5px') {
document.getElementById('id2D').style.top = "5px";
} else {
document.getElementById('id2D').style.top = "84px";
}} не работает еси поставить вместо гет елемент буквы а в и т.д. не работает чето не то пишу походу

новый вася
03.08.2016, 22:14
ща чекну сяп

рони
03.08.2016, 22:15
новый вася,
if(document.getElementById('id2O') !== null)

новый вася
03.08.2016, 22:17
новый вася,
function sortir() {
var a = document.getElementById("id2O");
var b = document.getElementById("id2D");
var c = document.getElementById("id2S");
if (a)
if (a.style.top != "5px") a.style.top = "5px";
else a.style.top = "44px";
if (b)
if (b.style.top != "5px") b.style.top = "5px";
else b.style.top = "64px";
if (c)
if (c.style.top != "5px") c.style.top = "5px";
else c.style.top = "84px";
return false
};
работает спасибо, жаль не работает плюсик кинуть от мя =(

warren buffet
03.08.2016, 23:29
sortir

Это по-французски значит "выйти".