2017-04-26 40 views
1

我有一個帶有id,div的div。它包含一個草圖(編程)地圖。我跟蹤鼠標點擊座標,以便我可以在點擊位置顯示工具提示。我需要在工具提示中包含signo [i]和status [i]的內容。你能告訴我如何在JavaScript中做到這一點?如何在javascript中單擊的位置顯示工具提示(內容存儲在數組中)?

代碼:

 <div id="lines" onclick="showCoords(event)"> 

     <script> 
b = xmlDoc.getElementsByTagName("TRACK"); 


jQuery.ajax({ 
       type: "POST", 
       url: "WebForm1.aspx/GetStations", //It calls our web method 
       contentType: "application/json; charset=utf-8", 
       data: JSON.stringify({ myArray1: dl_id_track, myArray2: dl_id_gate, myArray3: dl_id_point}), 
       dataType: "json", 
       async: true, 
       success: function (data) { 

       if (data != null) { 
        var len = data.d.length;             
         for (var i = 0; i < len; i++) { 
          signo[i] = data.d[i].signo; 
          status[i] = data.d[i].status; 
          } 

     function showCoords(event) { 
        var xcoord = event.clientX; 
        var ycoord = event.clientY; 
        var coords = "X coords: " + xcoord + ", Y coords: " + ycoord; 
        document.getElementById("demo1").innerHTML = coords; 
        for (var i = 0; i < b.length; i++) { 
         var x1 = parseInt(left_track[i]) + parseInt(x1_track[i]); 
         var y1 = parseInt(top_track[i]) + parseInt(y1_track[i]); 
         var x2 = parseInt(left_track[i]) + parseInt(x2_track[i]); 
         var y2 = parseInt(top_track[i]) + parseInt(y2_track[i]); 
             var slope = (y2 - y1)/(x2 - x1); 
         var line = (slope * xcoord) - ycoord - x1 + y1; 
         if (line == 0) { 
               asset = "track"; 
         } 
        } 

       } 

    </script> 
+0

好心地顯示您的代碼 – prasanth

+0

我已添加它。 – padma

+0

你沒有在代碼btw中的[j]和b [j] –

回答

0

使用標題屬性

下面的內容,這裏將顯示爲工具提示

function showCoords(event) { 
       var xcoord = event.clientX; 
       var ycoord = event.clientY; 
       var coords = "X coords: " + xcoord + ", Y coords: " + ycoord; 
       document.getElementById("demo1").innerHTML = coords; 
       for (var i = 0; i < b.length; i++) { 
        var x1 = parseInt(left_track[i]) + parseInt(x1_track[i]); 
        var y1 = parseInt(top_track[i]) + parseInt(y1_track[i]); 
        var x2 = parseInt(left_track[i]) + parseInt(x2_track[i]); 
        var y2 = parseInt(top_track[i]) + parseInt(y2_track[i]); 
            var slope = (y2 - y1)/(x2 - x1); 
        var line = (slope * xcoord) - ycoord - x1 + y1; 
        if (line == 0) { 
              asset = "track"; 
        } 
       } 
      document.getElementById("lines").title =coords; 
      } 

使用這種定位工具提示您

HTML-Tooltip position relative to mouse pointer

+0

title屬性用於html。我想在JavaScript中的點擊位置顯示工具提示。 – padma

相關問題