자바 스크립트를 ID에서 클래스로 변경하려면 어떻게해야합니까? 나는 혼자서하려고했지만 운이 없다면, 이것이 결국 내가 끝내는 것이다. 원래 코드는 요소를 표시하거나 숨기고 사용자 선택을 기억하는 것이 었습니다.자바 스크립트에서 ID를 클래스로 변경하는 방법
HTML :
이<div class="popup" style="display:none">
<img src="image-url.jpg" alt="alt text" title="title text">
</div>
JAVASCRIPT :
function setCookie (name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie (name) {
var cookie = " " + document.cookie;
var search = " " + name + "=";
var setStr = null;
var offset = 0;
var end = 0;
if (cookie.length > 0) {
offset = cookie.indexOf(search);
if (offset != -1) {
offset += search.length;
end = cookie.indexOf(";", offset);
if (end == -1) {
end = cookie.length;
}
setStr = unescape(cookie.substring(offset, end));
}
}
if (setStr == 'false') {
setStr = false;
}
if (setStr == 'true') {
setStr = true;
}
if (setStr == 'null') {
setStr = null;
}
return(setStr);
}
function hidePopup() {
setCookie('popup_state', false);
document.getElementsByClassName('popup').style.display = 'none';
}
function showPopup() {
setCookie('popup_state', null);
document.getElementsByClassName('popup').style.display = 'block';
}
function checkPopup() {
if (getCookie('popup_state') == null) {
// if popup was not closed
document.getElementsByClassName('popup').style.display = 'block';
}
}
저는 JavaScript에 익숙하지 않습니다. 새로운 코드를 붙여 넣기 만해도 될까요? 감사. –
하나의 함수에 대한 예제를 추가했습니다. (참고로, 나는 이름을 조금 변경했는데, 우리는 잠재적으로 하나 이상을 변경했기 때문이다.) 다른 함수의 경우, 변경 사항은 거의 동일 할 것이다. – cHao
요소를 표시하려면 * display *를 "block"으로 설정하지 말고 ""(빈 문자열)로 설정하여 함수가 기본 또는 상속 된 표시 값을 가진 요소를 올바르게 표시하도록합니다. – RobG