Jump to content
  • разработка интернет магазинов на opencart
  • доработка интернет магазинов на opencart

Помогите дописать функцию на JS


Recommended Posts

Написал икой скрипт:

<script type="text/javascript">
(function(){
	var t = document.getElementById("t1");
	window.addEventListener("load", function(){
		var color = window.sessionStorage.getItem("tableColor");
		if(color == 'undefined'){
			t.style.backgroundColor = "aliceblue";
		}else if(color == "green"){
			t.style.backgroundColor = color;
		}
	}, false);
})()
function eight(){
	var eight = document.getElementById("t1");
	eight.style.backgroundColor = "#ccc";
	window.sessionStorage.setItem("tableColor", "#ccc");
}
</script>

Суть его в том, что при клике на изображение он меняет его фон и осивляет его измененным при перезагрузке страницы. 
Подскажите, что нужно дописать, что бы при повторном клике фон возвращался в исходное состояние?

Edited by scandinav
Link to comment
Share on other sites


Говнокод:

var color = window.sessionStorage.getItem("tableColor");
if(color == '#ccc'){
    t.style.backgroundColor = "нужный цвет";
    window.sessionStorage.setItem("tableColor", "нужный цвет");
}else {
    t.style.backgroundColor = '#ccc';
    window.sessionStorage.setItem("tableColor", "#ccc");
}

 

А вообещё Гуглите js toggle class

 

Link to comment
Share on other sites


Похоже код Вы нагуглили
код нужно переписать, имхо
Принцип икой
Клик по кнопке - добавляем класс элементу, у этого класса свой цвет. Пишем сессию или куки. 
Пишем условие на проверку куки или сессии и при перезагрузке страницы осивляем класс. 
Условие - проверяем налилие элемент на налилие класса и куки - клик по кнопке - убираем класс и олищаем куку. 

Я бы по икому принципу пошел

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.