Javascript复选框以更改背景不工作
我试图使用复选框更改元素的背景位置,但不起作用 代码如下:Javascript复选框以更改背景不工作,javascript,checkbox,background-position,Javascript,Checkbox,Background Position,我试图使用复选框更改元素的背景位置,但不起作用 代码如下: x=false; 函数检查(){ if(x){ document.getElementById(“checkboz”).style.backgroundPosition='0'; document.getElementById(“checkboz”).style.backgroundPosition='-28px 0'; x=假; }否则{ document.getElementById(“checkboz”).style.backg
x=false;
函数检查(){
if(x){
document.getElementById(“checkboz”).style.backgroundPosition='0';
document.getElementById(“checkboz”).style.backgroundPosition='-28px 0';
x=假;
}否则{
document.getElementById(“checkboz”).style.backgroundPosition='none';
document.getElementById(“checkboz”).style.backgroundPosition='0';
x=真;
}
}
#checkboz{
背景:url(http://movimientomore.com/images/2016/12/21/sprite-check-01.png);
背景尺寸:55px;
背景重复:无重复;
宽度:28px;
高度:28px;
}
唱片公司
您的代码中没有eventlistener或onclick
document.getElementById("checkboz").addEventListener("click", Check);
x=false;
函数检查(){
x=!x;
如果(x){
document.getElementById(“checkboz”).style.backgroundPosition='0';
document.getElementById(“checkboz”).style.backgroundPosition='-28px 0';
}
否则{
document.getElementById(“checkboz”).style.backgroundPosition='none';
document.getElementById(“checkboz”).style.backgroundPosition='0';
}
}
document.getElementById(“checkboz”).addEventListener(“单击”,检查)代码>
#checkboz{
背景:url(http://movimientomore.com/images/2016/12/21/sprite-check-01.png);
背景尺寸:55px;
背景重复:无重复;
宽度:28px;
高度:28px;
}
唱片公司
您的代码中没有eventlistener或onclick
document.getElementById("checkboz").addEventListener("click", Check);
x=false;
函数检查(){
x=!x;
如果(x){
document.getElementById(“checkboz”).style.backgroundPosition='0';
document.getElementById(“checkboz”).style.backgroundPosition='-28px 0';
}
否则{
document.getElementById(“checkboz”).style.backgroundPosition='none';
document.getElementById(“checkboz”).style.backgroundPosition='0';
}
}
document.getElementById(“checkboz”).addEventListener(“单击”,检查)代码>
#checkboz{
背景:url(http://movimientomore.com/images/2016/12/21/sprite-check-01.png);
背景尺寸:55px;
背景重复:无重复;
宽度:28px;
高度:28px;
}
唱片公司
我已更新了您的小提琴,以显示您正在寻找的功能的工作版本
正如其他人在评论中所说的,Check函数从未被调用,但是另外,最好在元素上交换类,而不是尝试手动操作样式
var element = document.getElementById('modlgn-remember');
element.addEventListener('click', function(e) {
console.log(element.checked)
Check(element.checked);
}, false);
x=false;
function Check(checked){
console.log(x);
var $el = document.getElementById("checkboz")
if(checked){
$el.className = $el.className.replace(' remove-background- position-1 remove-background-position-2', '');
$el.className += ' add-background-position-1 add-background-position-2';
x=false;
} else {
var cn = $el.className;
cn = $el.className.replace(' add-background-position-1 add-background-position-2', '');
$el.className = cn;
$el.className += ' remove-background-position-1 remove-background-position-2';
x=true;
}
}
使用以下css类:
.add-background-position-1 {
background-position: 0 0 !important;
}
.add-background-position-2 {
background-position: -28px 0 !important;
}
.remove-background-position-1 {
background-position: inherit !important;
}
.remove-background-position-2 {
background-position: 0 0 !important;
}
没有这个!重要信息:在css类中,现有的背景样式将优先于新类
我希望这对你有帮助 我已更新了您的小提琴,以显示您正在寻找的功能的工作版本
正如其他人在评论中所说的,Check函数从未被调用,但是另外,最好在元素上交换类,而不是尝试手动操作样式
var element = document.getElementById('modlgn-remember');
element.addEventListener('click', function(e) {
console.log(element.checked)
Check(element.checked);
}, false);
x=false;
function Check(checked){
console.log(x);
var $el = document.getElementById("checkboz")
if(checked){
$el.className = $el.className.replace(' remove-background- position-1 remove-background-position-2', '');
$el.className += ' add-background-position-1 add-background-position-2';
x=false;
} else {
var cn = $el.className;
cn = $el.className.replace(' add-background-position-1 add-background-position-2', '');
$el.className = cn;
$el.className += ' remove-background-position-1 remove-background-position-2';
x=true;
}
}
使用以下css类:
.add-background-position-1 {
background-position: 0 0 !important;
}
.add-background-position-2 {
background-position: -28px 0 !important;
}
.remove-background-position-1 {
background-position: inherit !important;
}
.remove-background-position-2 {
background-position: 0 0 !important;
}
没有这个!重要信息:在css类中,现有的背景样式将优先于新类
我希望这对你有帮助 你怎么称呼你的函数?您根本没有调用函数?将onclick=“Check()”
添加到复选框中。永远不会调用Check()。如何调用您的函数?您根本没有调用函数?将onclick=“Check()”
添加到复选框中。永远不会调用Check()。更新了代码,使其嵌入到本文中,并更正了背景的预期切换值错误。(选中时选中,未选中时清除)。另一个代码让它在第一次检查时什么都不做,然后在复选框未选中时被选中。我对回答问题不熟悉,谢谢编辑。这个答案看起来更好:)@RahulDesai-我只是想请你添加这个(MDN链接),因为这是您在解决方案中提供的一件此解决方案没有并且可以使用的东西:)更新了代码,以便将其嵌入到本文中,并更正了一个与背景的预期切换值有关的错误。(选中时选中,未选中时清除)。另一个代码让它在第一次检查时什么都不做,然后在复选框未选中时被选中。我对回答问题不熟悉,谢谢编辑。这个答案看起来更好:)@RahulDesai-我只是想请您添加这个(MDN链接),因为这是您在解决方案中提供的一个东西,而这个解决方案没有并且可以使用:)