Не работает счетчик :(
Доброго времени суток! Хочу спросить, если кто нибудь в курсе - написал функцию (несколько громоздкую, но не в этом суть), которая должна по нажатию на кнопку делать <p> красными, по второму - черными. Однако не могу понять, почему этого не происходит, хотя я задаю глобальную переменную count - по идее все должно работать как часы
function selectAllp() {
if (count === null) var count =1;
if (count%2 !== 0){var pList = document.getElementsByTagName("p");
for (var i = 0; i < pList.length; i++) {
pList[i].setAttribute("style", "color:red");
}
count=2;
return
}
else if (count%2 == 0){var pList =
document.getElementsByTagName("p");
for (var i = 0; i < pList.length; i++) {
pList[i].setAttribute("style", "color:black");
}
}
count=1;
}
. Спасибо за ответ! |
Цитата:
|
Цитата:
|
Цитата:
Цитата:
|
Цитата:
pList[i].style.color='red'; |
ну дальше то идет - count=2 (т.е. глобальная). Даю весь код:
<html>
<head>
<title>Count</title>
</head>
<body>
<script type="text/javascript">
function selectAllp() {
if (count === null) var count =1;
if (count%2 !== 0){var pList = document.getElementsByTagName("p");
for (var i = 0; i < pList.length; i++) {
pList[i].setAttribute("style", "color:red");
}
count=2;
return
}
else if (count%2 == 0){var pList = document.getElementsByTagName("p");
for (var i = 0; i < pList.length; i++) {
pList[i].setAttribute("style", "color:black");
}
}
count=1;
}
</script>
<p>Привет, медвед0!</p>
<p>Привет, медвед1!</p>
<p>Привет, медвед2!</p>
<p>Привет, медвед3!</p>
<input type="button" onclick="selectAllp()" style="width: 210px;" value="Выбрать все параграфы" />
</body>
</html>
|
Цитата:
if (...) {
function f() { return true; }
} else {
function f() { return false; }
}
...
не люблю такие вопросы - они унылы... |
var count =1; поставь перед функцией
function selectAllp() { |
var count =1 / у тебя каждый раз заново объявляется и ей присваивается значение 1
|
Цитата:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=windows-1251' />
<!--
<script src='https://code.jquery.com/jquery-latest.js'></script>
<script src="https://code.angularjs.org/1.3.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.9/angular-route.js"></script>
-->
<style type='text/css'>
</style>
<script type='text/javascript'>
var count=false;
function selectAllp() {
var clr=(count)? 'black': 'red';
var pList = document.getElementsByTagName("p");
for (var i = 0; i < pList.length; i++) {
pList[i].style.color=clr;
}
count=!count;
}
</script>
</head>
<body>
<p>Привет, медвед0!</p>
<p>Привет, медвед1!</p>
<p>Привет, медвед2!</p>
<p>Привет, медвед3!</p>
<input type="button" onclick="selectAllp()" style="width: 210px;" value="Выбрать все параграфы" />
</body>
</html>
|
у одну строчку задача решается
<html>
<head>
<title>Count</title>
</head>
<body>
<p>Привет, медвед0!</p>
<p>Привет, медвед1!</p>
<p>Привет, медвед2!</p>
<p>Привет, медвед3!</p>
<input type="button" style="width: 210px;" value="Выбрать все параграфы" />
<script type="text/javascript">
document.querySelector("input").onclick =e=> document.querySelectorAll("p").forEach(el=>el.style.color = el.style.color === 'red'?'black':'red');
</script>
</body>
</html>
|
Цитата:
Цитата:
|
Цитата:
function selectAllp() {...}
;) |
Цитата:
|
| Часовой пояс GMT +3, время: 05:54. |