2016-05-19 23 views
0

我在phonegap构建中遇到了一个问题,这是一个运行两次(在手机上用phonegap构建)的函数,不同的是当我在firefox上执行脚本时。Js函数在phonegap上运行2次构建

那是我的代码的一部分:

$(".blackcase").click(function(e) 
{ 
    if (elemIsEmpty($(this)) && isSelected("black")) 
    { 
     return; 
    } 
    if (isSelected("white")) 
    { 
     //swap white to black 
     alert("swap b to w"); 
     if ($(this).html() == valueoffirst.html()) 
     { 
      $(valueoffirst.html("")); 
      alert("swap b to w1"); 
     } 
     else 
     { 
      swapValuesFromTo($("#" + selectedCase.id), $(this)); 
      alert("swap b to w 2"); 
     } 
     clearSelectedCase(); 
     removeSelectionWithRed(); 
     return; 
    } 
    if (isSelected("black")) 
    { 
     alert("swap b to b"); 
     $("#" + selectedCase.id).removeClass('red'); 
     if ($(this).html() == valueoffirst.html() && $(this).attr('id') != valueoffirst.attr('id')) 
     { 
      $(valueoffirst.html("")); 
      alert("swap b to b1"); 
     } 
     else 
     { 
      swapValuesFromTo($("#" + selectedCase.id), $(this)); 
      alert("swap b to b 2"); 
     } 
     clearSelectedCase(); 
     removeSelectionWithRed(); 
     return; 
    } 
    //alert("black is selected"); 
    selectWithRed($(this)); 
    updateSelectedCase("black", $(this).attr("id"), $(this).html()); 
    valueoffirst = $(this); 
}); 

function removeSelectionWithRed() 
{ 
    $('div').removeClass('red'); 
} 

function selectWithRed(element) 
{ 
    removeSelectionWithRed(); 
    element.addClass('red'); 
} 

function updateSelectedCase(color, id) 
{ 
    selectedCase.color = color; 
    selectedCase.id = id; 
} 

function moveValueFromTo(elemFrom, elemTo) 
{ 
    elemTo.html(elemFrom.html()); 
    setValueToElem("", elemFrom); 
} 

function setValueToElem(value, elem) 
{ 
    elem.html(value); 
} 

function swapValuesFromTo(elemFrom, elemTo) 
{ 
    var fromValue = elemFrom.html(); 
    var toValue = elemTo.html(); 
    setValueToElem(fromValue, elemTo); 
    setValueToElem(toValue, elemFrom); 
} 

function isSelected(color) 
{ 
    return selectedCase.color == color; 
} 

function clearSelectedCase() 
{ 
    selectedCase.color = ""; 
    selectedCase.id = ""; 
} 

function elemIsEmpty(elem) 
{ 
    return elem.html().length == 0; 
} 

当我运行我的Firefox代码,如果我上的黑色外壳,然后双击另一个blackcase,它的交换价值,我有两个警报:

交换的b to b

交换b键B2

一切正常,但我的手机和我的phongap版本测试,如果我在blackcase单击然后又blackcase值不换,我有4个警报:

交换的B to B

交换b键B2

交换的B to B

交换b键B2

因此,我认为值交换但重新交换,因此值与起始值相同(如没有任何更改)。

非常感谢您的帮助!

+0

听起来就像是在不停地添加您的点击处理程序。这里是'$(“。blackcase”)。click(function(){})' –

+0

'请调用,谢谢,是的,但我没有承认为什么它在Firefox上没问题,有一个用于phonegap或另一个js规范想这样? – Sanalol

回答

2

也许它在移动平台上运行时也触发了触摸事件。尝试注册触摸事件来测试它是否被触发。

+0

感谢您的回答,我应该尝试通过触摸来取代点击事件? – Sanalol

+0

如果您是为移动平台开发的,点击应该替换为触摸事件。 – Choo