2014-05-15 3 views
0

Rails 애플리케이션에 JQuery 기능을 추가하려고합니다. 그러나 작동하지 않습니다. JQuery draggable을 사용하여 화면에서 이미지를 이동합니다. 독립형 HTML 파일로 작업하고 있지만 레일스 응용 프로그램에 추가하는 데 많은 어려움을 겪고 있습니다. 이것은 독립 실행 형 파일 : 나는 자산/자바 스크립트/custom_map.js에서 자바 스크립트를 배치 한JQuery 기능 추가

<!DOCTYPE html> 
<html> 
<head> 
    <title>City</title> 
    <meta charset="UTF-8"> 
    <link rel="stylesheet" href="js/jquery-ui-1.10.4/themes/base/jquery-ui.css"> 
    <script type="text/javascript" src="js/jquery-ui-1.10.4/jquery-1.10.2.js"></script> 
    <script type="text/javascript" src="js/jquery-ui-1.10.4/ui/jquery-ui.js"></script> 

    <style type="text/css"> 

    h1, h3 
    { 
    text-align: center; 
    margin: 0px; 
    } 

    /* style the list to maximize the droppable hitarea */ 
    #canvas 
    { 
    width: 600px; 
    height: 600px; 
    border: 1px solid black; 
    margin: 0px auto; 
    overflow: hidden; 
    } 
    /*#canvas ol { margin: 0; padding: 1em 0 1em 3em; }*/ 
    /*li { list-style: none;}*/ 
    .canvas-area 
    { 
    width: 600px; 
    height: 600px; 
    } 

    /* map background */ 
/* img { 
     position: relative; 
     left: 50%; 
     top: 50%; 
    }*/ 

    /* draggable icons */ 
    .draggable 
    { 
    background: transparent url('images/transparent.png') repeat center top; 
    width: 40px; 
    height: 40px; 
    padding: 5px; 
    float: left; 
    margin: 0 10px 10px 0; 
    font-size: .9em; 
    border: hidden; 
    /*position: absolute;*/ 
    } 
    #draggable 
    { 
    width: 40px; 
    height: 40px; 
    } 
    div img 
    { 
    margin: 0px auto; 
    position: relative; 
    top: 0px; 
    left: 0px; 
    width: 40px; 
    height: 40px; 
    } 

    .arrow img 
    { 
    width: 200px; 
    height: 200px; 
    } 
    .arrow 
    { 
    width: 200px; 
    height: 200px; 
    border: 1px solid black; 
    } 
    .commercial100 img 
    { 
    width: 100px; 
    height: 100px; 
    } 
    .commercial100 
    { 
    width: 100px; 
    height: 100px; 
    } 

    #building img 
    { 
    height: 150px; 
    } 

    </style> 

    <script> 

    // jquery ui script for snapping to grid 
    $(function() { 
     $(".draggable").draggable({ grid: [10,10] }); 
     $("#draggable4").draggable({ grid: [ 20, 20 ], snap: true }); 
    }); 

    </script> 
</head> 
<body> 
<div class="content"> 

    <!-- Canvas for building the city --> 
    <h1>Mock view of my city</h1> 
    <h3>600 x 600</h3> 
    <div id="canvas"> 

    <div id="ignoreThis" class="ui-widget-content canvas-area"> 

     <p class="placeholder">Add your items here</p> 

     <div id="arrowSvg" class="draggable ui-widget-content arrow"> 
     <img src="images/arrow.svg" alt="house" type="image/svg+xml" /> SVG (200px x 200px) 
     </div> 

     <div id="arrowPng" class="draggable ui-widget-content arrow"> 
     <img src="images/arrow.png" alt="house"> PNG (200px x 200px) 
     </div> 

     <div id="building" class="draggable ui-widget-content commercial100"> 
     <img src="images/building.png" alt="building" /> 
     </div> 

     <div id="factory" class="draggable ui-widget-content commercial100" > 
     <img src="images/factory.png" alt="factory" /> 
     </div> 

     <div id="ferry" class="draggable ui-widget-content commercial100" > 
     <img src="images/ferry.png" alt="ferry" /> 
     </div> 

     <div id="house2l" class="draggable ui-widget-content" > 
     <img src="images/house2l.png" alt="two level house" /> 
     </div> 

     <div id="houseSvg" class="draggable ui-widget-content"> 
     <img src="images/house.svg" alt="house" type="image/svg+xml" /> SVG 
     </div> 

     <div id="housePng" class="draggable ui-widget-content"> 
     <img src="images/house.png" alt="house" /> 
     </div> 

     <div id="houseSF" class="draggable ui-widget-content" > 
     <img src="images/housesf.png" alt="SF house" /> 
     </div> 

     <div id="street1" class="draggable ui-widget-content street" > 
     <img src="images/street.png" alt="street"> 
     </div> 

     <div id="street2" class="draggable ui-widget-content street" > 
     <img src="images/street.png" alt="street"> 
     </div> 

     <div id="street3" class="draggable ui-widget-content street" > 
     <img src="images/street.png" alt="street"> 
     </div> 

     <div id="street4" class="draggable ui-widget-content street" > 
     <img src="images/street.png" alt="street"> 
     </div> 

    </div> 
    <script> 
    // code to make the active div move to the front 
    // code from http://www.codingforums.com/javascript-programming/260289-bring-div-element-front-click.html 

     // create an array to hold the (buildings, streets, landmarks) element's id 
     var ids=[], 
     // grab all the divs (each icon) and put it into thedivs 
     thedivs = document.getElementById("canvas").getElementsByTagName("div"); 

     for (var i = 0; i < thedivs.length; i++) { 

     // add the id of each div to the ids array 
     //alert(thedivs[i].id); 
     ids.push(thedivs[i].id); 


     thedivs[i].onclick = function() { 

      for (var a = 0; a < ids.length; a++) { 
      if (this.id == ids[a]) { 
       // take current id that matches the selected id and move it to the end 
       ids.push(ids.splice(a,1)); 
      } 
      document.getElementById(ids[a]).style.zIndex=a;  
      } 
     } 
     } 
    </script> 
    </div> 
    <input type="button" value="save" onclick="saveAction()"> 

    <script> 
     //Cycle through images and grab their x/y positions 

     var saveAction = function(){ 

     elementNames = document.getElementById("canvas").getElementsByTagName("div"); 
     for(var i = 0; i < elementNames.length; i++) 
     { 
     var idName = $("#" + elementNames[i].id); 


     var offset = idName.offset(); 
     alert("Left pos: " + offset.left + " Top pos: " + offset.top); 
     } 


    }; 
    </script> 
</div> 
</body> 
</html> 

그리고 내 new_map.html.erb보기에서 HTML/CSS를 배치했다. 이미지는 Rails 앱에서 잘 보이지만 드래그 할 수는 없습니다. 이 문제를 어떻게 해결할 수 있습니까? 감사!

+1

JS를보기에도 추가 했습니까? – Zhihao

+0

무슨 뜻인가요? – Whoppa

+0

JS를 눈에 띄지 않게 포함 시키면 JS에 어떤 문제가 있습니까? –

답변

1

아마도로드 순서가 잘못되었거나 자산 경로가 잘못되었을 수 있습니다. 페이지에서 마지막으로 스크립트를 연결하는 시도하고 JQuery와 준비가 함수 내에서 그 모든 것을 포장 :

$(function() { 
     // all your scripts here 
    }); 

은 또한 당신의 .js 파일 그것은해야 제대로 제공되어 있는지 확인 소스를보기 및 만들기 시도//assets/javascripts/yourjs.js가 아닌 assets/yourjs.js

또한 jquery UI 스택 함수를 사용하여 항목을 맨 위에 놓을 수 있다고 생각합니다 (잘 모르겠 으면 시도해보십시오).) :

$(".draggable").draggable({ stack: ".draggable" }); 
+0

당신이 말하는 페이지가 어떤 것인지 나는 잘 모르겠습니다. 보기 ... new_map.js? – Whoppa

+0

스크립트 하단에 링크를 추가하십시오. –

+0

여전히 작동하지 않습니다. 페이지 하단에 을 추가했으며 $ (function) {} 내에 내 jquery를 new_map.js에 추가했습니다. – Whoppa

0

로 알려진 (


Unobtrusive

당신은 HTML 코드에서 자바 스크립트를 넣어해서는 안 : pecific 오류가 당신은 내가 당신은 문제 만 세부가 발생하는 이유를 수신하고, 어떻게 일반적으로 그것을 해결하기 위해 인라인 JS). 문제는 지저분하다는 인 & 쉽게 ...이 때문에 상대적으로 안전하지 않은, 일반적 아마추어로 간주하게 변조 될 수

더 좋은 방법은 JS는 다루지로드 파일에 저장된다 unobtrusive JS을 (사용하는

HTML). 당신이 당신의 views에서 JS를 마이그레이션하고 asset 파일에 넣으면 우리에게 우리가 다음하는 데 사용할 수있는 피드백을 제공하기 위해 당신은 훨씬 더 나은 위치에있을거야,

#app/assets/javascripts/application.js 
var ready = function() { // -> you said JQuery :) 
     $(".draggable").draggable({ grid: [10,10] }); 
     $("#draggable4").draggable({ grid: [ 20, 20 ], snap: true }); 

     ///////////////////////////////// 

     var ids=[], 
     // grab all the divs (each icon) and put it into thedivs 
     thedivs = document.getElementById("canvas").getElementsByTagName("div"); 

     for (var i = 0; i < thedivs.length; i++) { 

     // add the id of each div to the ids array 
     //alert(thedivs[i].id); 
     ids.push(thedivs[i].id); 


     thedivs[i].onclick = function() { 

      for (var a = 0; a < ids.length; a++) { 
      if (this.id == ids[a]) { 
       // take current id that matches the selected id and move it to the end 
       ids.push(ids.splice(a,1)); 
      } 
      document.getElementById(ids[a]).style.zIndex=a;  
      } 
     } 
     } 
}; 
$(document).on("page:load ready", ready); 

: 레일이에 대한 asset pipeline를 사용 문제를 더욱 해결하십시오