不能将变量的值从true更改为false
Cannot change value of variable from true to false
现在更新,我发现我需要禁用功能更改一段时间,所以我使var启用并使功能仅在var enabled == true
时可用,在我传递enabled = false;
的功能结束时,它应该禁用功能change();
,在第一次之后,但它仍然工作完美。有人能帮帮我吗?
//This function will give me the ID of the images, so i can make my conditions according to the ID
var enabled = true;
function pegaId(obj){
var idCorreto = obj.getAttribute('id');
return idCorreto;
}
if(enabled == true){
function drag(obj){
var zIndexImg = 0;
//with the var id, i detect which image is being dragged by the id
var id = obj.getAttribute('id');
var img = document.getElementById(id);
var imgPositionLeft = img.offsetLeft;
var imgPositionTop = img.offsetTop;
img.ondragstart = function(){
return false;
};
function dropImage(e){
img.style.transition = "";
img.style.zIndex = zIndexImg++;
//make the drag of the imgs
img.style.top = e.clientY - imgPositionTop +'px';
img.style.left = e.clientX - imgPositionLeft + 'px';
console.log(e.clientX - imgPositionLeft);
}
function change(id1,id2,div1,div2){
//this function will make the change of the imgs
img.removeAttribute('style');
//i put the imgs and the divs they are kept in these vars
var imgDiv = document.getElementById(div1);
var imgDiv_2 = document.getElementById(div2);
var imgId = document.getElementById(id1);
var imgId2 = document.getElementById(id2);
//take the id of both imgs
var getId = imgId.getAttribute('id');
var getId2 = imgId2.getAttribute('id');
//and change the id's
imgId.setAttribute('id',getId2);
imgId2.setAttribute('id',getId);
//and overwrite the imgs in the divs, making the change
imgDiv.innerHTML = imgId2.cloneNode().outerHTML;
imgDiv_2.innerHTML = imgId.cloneNode().outerHTML;
}
function drop(e){
//this function is for the drop of the img
dropImage(e);
//remove the added events
document.removeEventListener('mousemove' ,dropImage);
document.removeEventListener('mouseup', drop);
//find out which image is clicked so i can make the right change
if(img.style.left >= '90px' && img.style.left <= '130px'){
//take the ID so i know which image are being dragged
if(pegaId(obj) == 'teste1'){
//and make the change
change("teste1","teste2","img1","img2");
}else if(pegaId(obj) == 'teste2'){
change("teste2","teste3","img2","img3");
}
}
if(img.style.left >= '230px' && img.style.left <= '250px'){
change("teste1","teste3","img1","img3");
}
if(img.style.left >= '-115px' && img.style.left <= '-130px'){
if(pegaId(obj) == 'teste3'){
change("teste2","teste3","img2","img3");
}
else if(pegaId(obj) == 'teste2'){
change("teste2","teste1","img2","img1");
}
}
if(img.style.left >= '-225px' && img.style.left <= '-250px'){
change("teste1","teste3","img1","img3");
}
//reset the values after the change
img.style.left = '0px';
img.style.top = '0px';
}
//add the events again when the img is clicked
img.addEventListener('mousedown', function(){
document.addEventListener('mousemove', dropImage);
document.addEventListener('mouseup', drop);
});
}
enabled = false;
}
http://jsfiddle.net/m8vvhf8e/3/我发现我的问题是我在改变图像的同一函数中改变了ID,这给函数带来了一些麻烦。我能够通过在一个函数中更改图像和在另一个函数中更改ID来解决问题:
//function to change the imgs places
function change(id1,id2,div1,div2){
img.removeAttribute('style');
var imgDiv = document.getElementById(div1);
var imgDiv_2 = document.getElementById(div2);
var imgId = document.getElementById(id1);
var imgId2 = document.getElementById(id2);
imgDiv.innerHTML = imgId2.cloneNode().outerHTML;
imgDiv_2.innerHTML = imgId.cloneNode().outerHTML;
}
//function to change the imgs id's
function trocaId(id1,id2){
var id_1 = document.getElementById(id1);
var id_2 = document.getElementById(id2);
id_1.setAttribute('id',id2);
id_2.setAttribute('id',id1);
}
//making the changes
if(img.style.left >= '250px' && img.style.left <= '290px'){
if(pegaId(obj) == 'teste1'){
change("teste1","teste2","img1","img2");
trocaId("teste1","teste2");
}else if(pegaId(obj) == 'teste2'){
change("teste2","teste3","img2","img3");
trocaId("teste2","teste3");
}
http://jsfiddle.net/v1u1xnwj/10/相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- JavaScript-切换“;全部检查”;复选框true/false
- (false==false==true)怎么可能是真的
- Javascript If else 只返回 TRUE 或只返回 FALSE
- 过滤器返回true或false
- 什么是'true'和'false'和'null'
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作
- 在JavaScript中为true==1和false==0
- true==false以某种方式计算为true
- 获取Javascript中单选按钮的true或false
- 使用!而in运算符在应该为true时返回false,为什么
- 如何使以下函数返回true或false
- 为什么 javascript 返回对象位置 true 或 false
- 如何使函数将属性值从 FALSE 设置为 TRUE
- 为什么 1.2 == true 返回 false 如果布尔值 (1.2) 实际上是真的
- 在 JavaScript 中,为什么 []==false 为 true
- 为什么true/true是1,true/false是无穷大,false/true是0,false/false是NaN
- Javascript 布尔值:false && true 结果为 true
- 覆盖保留字(设置 false = true)
- 为什么这个类不返回false|| true