2016-01-13 66 views
0
setTimeout(function() { 
     $.ajax({ 
     url : "handlers/H_AnnotationHandler.php", 
     data : "case_id=<?=$case_id?>&plink=<?=$plink?>&mode=get", 
     type : "post", 
     dataType : "json", 
     success : function (response) { 
      if (!response.error) { 
       annotation_length = response.annots.length; 
       for (var i = 0; i < response.annots.length; i++) { 
        var elt = document.createElement("div"); 
        elt.id = "runtime-overlay" + i; 
        elt.className = "highlight"; 
        viewer.addOverlay({ 
         element: elt, 
         location : viewer.viewport.imageToViewportRectangle(parseInt(response.annots[i].rect_x), parseInt(response.annots[i].rect_y), parseInt(response.annots[i].rect_w), parseInt(response.annots[i].rect_h)) 
        }); 
        $("#runtime-overlay"+i).attr("onclick", "$.clickOverlay('"+i+"')"); 
       } 
      }    
     } 
    }); 
    }, 3000); 

    $.clickOverlay = function(whichOverlay) { 
      var flag = 0; 
      $("#runtime-overlay"+whichOverlay).addEventListener("mousedown", function(){ 
       flag = 0; 
      }, false); 
      $("#runtime-overlay"+whichOverlay).addEventListener("mousemove", function(){ 
       flag = 1; 
      }, false); 
      $("#runtime-overlay"+whichOverlay).addEventListener("mouseup", function(){ 
       if(flag === 0){ 
        console.log("click"); 
       } 
       else if(flag === 1){ 
        console.log("drag"); 
       } 
      }, false); 
    } 

为什么我得到addeventlistener的类型错误? 你能帮助我,我试着理解点击或拖动。 所以我说,在我的单击事件功能: How to distinguish mouse "click" and "drag"

错误:遗漏的类型错误:$(...)的addEventListener不是一个函数

+4

你用JavaScript混合jQuery的,无论是在( 'mouseup''或'的document.getElementById(' 身份证')使用'的addEventListener('。 – Tushar

回答

2

作为一条评论指出,你正在尝试使用“香草”javascript方式在jQuery对象上添加一个eventlistener。

$.clickOverlay = function(whichOverlay) { 
     var flag = 0; 
     $("#runtime-overlay"+whichOverlay).addEventListener("mousedown", function(){ 
      flag = 0; 
     }, false); 
     $("#runtime-overlay"+whichOverlay).addEventListener("mousemove", function(){ 
      flag = 1; 
     }, false); 
     $("#runtime-overlay"+whichOverlay).addEventListener("mouseup", function(){ 
      if(flag === 0){ 
       console.log("click"); 
      } 
      else if(flag === 1){ 
       console.log("drag"); 
      } 
     }, false); 
} 

,而不是尝试:

$.clickOverlay = function(whichOverlay) { 
     var flag = 0; 
     $("#runtime-overlay"+whichOverlay).on("mousedown", function(){ 
      flag = 0; 
     }); 
     $("#runtime-overlay"+whichOverlay).on("mousemove", function(){ 
      flag = 1; 
     }); 
     $("#runtime-overlay"+whichOverlay).on("mouseup", function(){ 
      if(flag === 0){ 
       console.log("click"); 
      } 
      else if(flag === 1){ 
       console.log("drag"); 
      } 
     }); 
} 
1

addEventListener是侦听事件的JavaScript的方式,但是你在一个JQuery对象上调用它。 查看JQuery.on()以使用JQuery管理事件。

相关问题