Заменить getElementById на getElementsByClassName
Приветствую! Подскажите как правильно заменить getElementById на getElementsByClassName в нижеуказанном скрипте. Это скрипт GDPR Cookie, который имеет 2 кнопки (1-принять и 2-закрыть). Они обе должны срабатывать одинаково, однако, мы знаем, что не бывает одинаковый ID у двух элементов на странице, поэтому хотелось бы заменить id на class.
(function(){ "use strict"; window.FS = window.FS || {}; window.FS.GDPR = window.FS.GDPR || {}; //setup hook for when accept is clicked var onAcceptCbs = []; window.FS.GDPR.onAccept = function(cb){ onAcceptCbs.push(cb); }; //get the banner var banner = document.getElementById('GdprCookieBanner'); var acceptButton = document.getElementById('GdprCookieBannerAccept'); acceptButton.onclick = function(event){ //save the acceptance cookie var d = new Date(); d.setTime(d.getTime() + (365*24*60*60*1000)); //figure out the domain var cookieDomain = window.location.hostname; var domainParts = cookieDomain.split('.'); var firstPart = domainParts[0]; if( !isNumeric(firstPart) && (cookieDomain.indexOf("local")<0) && (domainParts.length > 1) ){ cookieDomain = '.' + domainParts.slice(domainParts.length -2).join('.'); } //actually save the cookie value document.cookie = 'gdprAccepted=true;path=/;expires=' + d.toUTCString() + ';domain='+cookieDomain; banner.style.display = "none"; //trigger any callbacks that have registered for when accept is clicked onAcceptCbs.forEach(function(cb){ cb(); }); //mark that it's been accepted window.FS.GDPR.accepted = true; //stops the click return false; }; if(document.cookie.indexOf('gdprAccepted=true') < 0){ //cookie not found. show banner banner.style.display = "block"; //mark that we don't have acceptance window.FS.GDPR.accepted = false; }else{ //mark that window.FS.GDPR.accepted = true; } function isNumeric(item){ return !isNaN(parseFloat(item)) && isFinite(item); } })(); Спасибо! :) |
Цитата:
<button class='test'>Item</button> <button class='test'>Item</button> <script> var o=document.getElementsByClassName('test'); o[0].innerHTML='Item 0'; o[1].innerHTML='Item 1'; </script> |
Можно попросить связать это с кодом, который указан в первом посте? А то не понятно что там на что заменить.
|
Please :)
|
Можно не менять на на getElementsByClassName
(function(){ "use strict"; window.FS = window.FS || {}; window.FS.GDPR = window.FS.GDPR || {}; //setup hook for when accept is clicked var onAcceptCbs = []; window.FS.GDPR.onAccept = function(cb){ onAcceptCbs.push(cb); }; const handler = function(event){ //save the acceptance cookie var d = new Date(); d.setTime(d.getTime() + (365*24*60*60*1000)); //figure out the domain var cookieDomain = window.location.hostname; var domainParts = cookieDomain.split('.'); var firstPart = domainParts[0]; if( !isNumeric(firstPart) && (cookieDomain.indexOf("local")<0) && (domainParts.length > 1) ){ cookieDomain = '.' + domainParts.slice(domainParts.length -2).join('.'); } //actually save the cookie value document.cookie = 'gdprAccepted=true;path=/;expires=' + d.toUTCString() + ';domain='+cookieDomain; banner.style.display = "none"; //trigger any callbacks that have registered for when accept is clicked onAcceptCbs.forEach(function(cb){ cb(); }); //mark that it's been accepted window.FS.GDPR.accepted = true; //stops the click return false; }; //get the banner var banner = document.getElementById('GdprCookieBanner'); var acceptButton = document.getElementById('GdprCookieBannerAccept'); acceptButton.onclick = handler; baner.onclick = handler; if(document.cookie.indexOf('gdprAccepted=true') < 0){ //cookie not found. show banner banner.style.display = "block"; //mark that we don't have acceptance window.FS.GDPR.accepted = false; }else{ //mark that window.FS.GDPR.accepted = true; } function isNumeric(item){ return !isNaN(parseFloat(item)) && isFinite(item); } })(); |
Спасибо! Но cookies теперь не запоминаются( после перезагрузки страницы опять сообщение о cookies отображается.
|
Часовой пояс GMT +3, время: 04:15. |