다음 스크립트를 사용하여 내 페이지의 색상 테마를 설정합니다. 사용자가 버튼을 클릭하면 HTML에 클래스가 설정됩니다. 사용자가 다른 버튼을 클릭하면 해당 클래스 이름이 대신 설정됩니다. 이것은 괜찮습니다.Javascript의 다른 버튼을 라디오 버튼 효과처럼 비활성화하고 활성화하려면 어떻게합니까?
<script type="text/javascript">
if (localStorage.buttonColor) {
document.getElementsByTagName('html')[0].className = localStorage.buttonColor
}
function getButtonColor(buttonName) {
localStorage.buttonColor = buttonName;
document.getElementsByTagName('html')[0].className = buttonName
}
</script>
버튼 색상에 대한 localstorage 값이있는 경우 클래스를 설정합니다. 그러나 버튼을 사용하지 않도록 설정해야합니다. 어떻게하면 localstorage의 값에 따라 버튼을 비활성화 할 수 있습니까?
또한 사용자가 버튼을 클릭하면 해당 버튼을 비활성화하고 "테마"클래스를 가진 다른 모든 버튼을 활성화 할 수 있습니다. 우리는 자바 스크립트 전용 솔루션을 찾고 있습니다.
다음은 HTML입니다 :
<form class="ng-pristine ng-valid">
<button class="theme" name="darkBlue" onclick="getButtonColor(this.name)">Blue</button>
<button class="theme" name="black" onclick="getButtonColor(this.name)">Black</button>
</form>
여기 하나 개의 솔루션입니다하지만 우리가 지금 버튼을 쉽게을 선택 할 수 있도록하는 클래스 이름을 부여하기로 결정으로이 필요할 꽤 모두가 아니다. 또한 로컬 저장소에서 가져 오는 경우에만 작동하며 사용자가 해당 버튼을 클릭하고 다른 모든 버튼을 사용하도록 설정 한 경우 버튼을 사용 중지해야합니다. 해제 버튼
if (localStorage.buttonColor) {
document.getElementsByTagName('html')[0].className = localStorage.buttonColor
var buttons = document.body.getElementsByTagName('form')[0].getElementsByTagName('button')
for(var i =0; i < buttons.length; i++)
if(buttons[i].name == localStorage.buttonColor) button.disabled = true
}
<폼 클래스 = "NG-깨끗한 NG-유효"> <버튼 ID = "darkBlue"클래스 = "테마"이름 = "darkBlue"onclick을 = "getButtonColor (이)"> 블루 <버튼 ID = "블랙"클래스 = "테마"이름 = "블랙"의 onclick = "getButtonColor (이)"> 블랙 –