【问题标题】:j query event handlersjquery 事件处理程序
【发布时间】:2014-09-10 13:19:10
【问题描述】:

我有一个 svg 的路线图,有 11 个站点。车站用圆圈表示,所以我有 11 个圆圈。我需要在 jquery 中执行以下操作的代码:-

  1. 应将单击的第一个圆圈视为源。
  2. 点击的第二个圆圈应被视为目的地。
  3. 此后单击任何圆圈都会弹出已选择源和目标的 js。
  4. 在确认来源和目的地时,我将加载一个包含详细信息的 div(我可以通过 ajax 调用来完成)

SVG 代码附在上面。 我有 php 代码从数据库中获取所有内容。 帮我解决这个问题!!

      !doctype html>
          <html lang="en">
             <head>
               <meta charset="utf-8">


              </head>

         <body>
 <object data="route.svg" type="image/svg+xml" id="route_svg" width="100%" height="100%">       </object>

  <svg xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"

      width="1000"
      height="1000"
      viewBox="-350 -100 1000 1000">

     <!--Versova-->
       <text x="60" y="40">Versova</text>
       <circle cx="40" cy="40" r="8" class="station" id="versova"
        style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>
        <line x1="43" y1="45" x2="92" y2="112" style="stroke:#006600; stroke-width:3"/>

      <!--DN Nagar-->
       <text x="100" y="95">D N Nagar</text>
      <circle cx="90" cy="110" r="8" class="station" id="dnnagar"
      style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>    
       <line x1="95" y1="115" x2="141" y2="140" style="stroke:#006600; stroke-  width:3"/>

      <!--Azad Nagar-->
       <text x="162" y="137">Azad Nagar</text>
        <circle cx="141" cy="138" r="8" class="station" id="azadnagar"
        style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>    
       <line x1="146" y1="141" x2="238" y2="188" style="stroke:#006600; stroke-width:3"/>

    <!--Andheri-->
       <text x="160" y="192">Andheri</text>
       <circle cx="230" cy="186" r="8" class="station" id="andheri"
       style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>    
       <line x1="236" y1="188" x2="272" y2="205" style="stroke:#006600; stroke-width:3"/>   

      <!--WEH-->
      <text x="294" y="205">WEH</text>
      <circle cx="280" cy="207" r="8" class="station" id="weh"
       style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>    
        <line x1="286" y1="210" x2="332" y2="230" style="stroke:#006600; stroke-width:3"/>        

      <!--Chakala-->
      <text x="265" y="240">Chakala</text>
       <circle cx="332" cy="230" r="8" class="station" id="chakala"
       style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>    
       <line x1="337" y1="233" x2="366" y2="244" style="stroke:#006600; stroke-width:3"/>         

     <!--Airport-->
       <text x="394" y="240">Airport</text>
        <circle cx="374" cy="246" r="8" class="station" id="airport"
        style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>    
      <line x1="380" y1="250" x2="415" y2="270" style="stroke:#006600; stroke-width:3"/>         

      <!--Marol-->
       <text x="360" y="280">Marol</text>
       <circle cx="415" cy="270" r="8" class="station" id="marol"
       style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>    
       <line x1="420" y1="274" x2="460" y2="298" style="stroke:#006600; stroke-width:3"/>       

      <!--Saki Naka-->
       <text x="388" y="320">Saki Naka</text>
       <circle cx="460" cy="300" r="8" class="station" id="sakinaka"
       style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>  
       <line x1="465" y1="307" x2="490" y2="347" style="stroke:#006600; stroke-width:3"/>       

      <!--Asalpha-->
      <text x="510" y="350">Asalpha</text>
      <circle cx="490" cy="350" r="8" class="station" id="asalpha"
       style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/>  
       <line x1="495" y1="356" x2="535" y2="385" style="stroke:#006600; stroke-width:3"/>   

      <!--Ghatkopar-->
      <text x="448" y="390">Ghatkopar</text>
      <circle cx="530" cy="380" r="8" class="station" id="ghatkopar"
       style="stroke:#006600;
       stroke-width: 3;
       fill:#00cc00"/> 

     </svg>

  </body>

   </html>

【问题讨论】:

  • 你太棒了!

标签: jquery jquery-svg


【解决方案1】:

这是 jsfiddle:http://jsfiddle.net/fTaz3/

js:

var src = '';
var dest = '';

$('circle').click(function () {
    if (src === '') {
        src = $(this).attr('id');
        alert('Source selected : ' + src);
        return;
    }
    if (dest === '') {
        dest = $(this).attr('id');
        alert('Destn selected : ' + dest);
        return;
    }
    alert('source[' + src + '] and destination[' + dest + '] already selected');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 2013-07-04
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多