如何使用嵌套在HTML中的$(document).ready()来调用函数

时间:2015-12-06 23:00:48

标签: javascript html

好的,所以我有这个让我拖放的功能。我让它在一个页面上工作,但我也希望在其他页面上使用此功能。所以我调整了函数来获取参数(我想要拖动的元素的ID和droptarget)并用作参数,所以我没有为每个页面编写单独的函数。我无法获取它上班。我对javascript,jquery mobile和html相当陌生。这甚至合法吗?如果是这样我该怎么办呢?

 <div data-role="page" id="proteinDrag" data-add-back-btn="true" data-dialog="true">
        <div data-role="header" style="background:#0493e5">
            <h1 style="color:white;">Protein</h1>
        </div>
        <p>Drag the food that is a good source of protein to the plate</p>
        <div id="droppable" class="ui-widget-header"><img src="....png"></div>
        <br style="clear:both">
        <div id="yesDrag" class="draggable ui-widget-content" style="text-align:middle;"><img src="....png"></div>
        <div id="noDrag" class="draggable ui-widget-content" style="text-align:left;"><img src="....png"></div>
        <script>
            dragging("#yesDrag","#noDrag","#droppable");
        </script>
  </div>
    //tried placing the script at end of the body and head     
  <script type ="text/javascript">
     var paraC;
     var paraW;
     var correct;
     var wrong;

     function dragging(idDrag, idNoDrag, idDrop){
         paraC = document.createElement("p");
         paraW = document.createElement("p");
         correct = document.createTextNode("Good Choice!");
         wrong = document.createTextNode("Not Quite!");
         $(function() {
           $( idDrag ).draggable({
                                    drag: function(event, ui){
                                      paraC.appendChild(correct);
                                      idDrag.appendChild(paraC);
                                      idDrop.appendChild(idDrag);
                                    }
                                 });
           $( idNoDrag ).draggable({
                                    helper:"clone",
                                    revert: true,
                                    drag: function(event, ui){
                                      paraW.appendChild(wrong);
                                      idNoDrag.appendChild(paraW);
                                    }
                                   });

           });
     }
 </script>

2 个答案:

答案 0 :(得分:1)

在宣布之前不要打电话给你的功能;)

我认为这就是为什么你没有得到任何东西......

<div data-role="page" id="proteinDrag" data-add-back-btn="true" data-dialog="true">
  <div data-role="header" style="background:#0493e5">
    <h1 style="color:white;">Protein</h1>
  </div>
  <p>Drag the food that is a good source of protein to the plate</p>
  <div id="droppable" class="ui-widget-header"><img src="....png"></div>
  <br style="clear:both">
  <div id="yesDrag" class="draggable ui-widget-content" style="text-align:middle;"><img src="....png"></div>
  <div id="noDrag" class="draggable ui-widget-content" style="text-align:left;"><img src="....png"></div>
</div>
//tried placing the script at end of the body and head     
<script type ="text/javascript">
var paraC;
var paraW;
var correct;
var wrong;

function dragging(idDrag, idNoDrag, idDrop){
 paraC = document.createElement("p");
 paraW = document.createElement("p");
 correct = document.createTextNode("Good Choice!");
 wrong = document.createTextNode("Not Quite!");
 $(function() {
   $( idDrag ).draggable({
    drag: function(event, ui){
      paraC.appendChild(correct);
      idDrag.appendChild(paraC);
      idDrop.appendChild(idDrag);
    }
  });
   $( idNoDrag ).draggable({
    helper:"clone",
    revert: true,
    drag: function(event, ui){
      paraW.appendChild(wrong);
      idNoDrag.appendChild(paraW);
    }
  });

 });
}
// lunch function
  dragging("#yesDrag","#noDrag","#droppable");
</script>

答案 1 :(得分:0)

我真的想通了,有点儿。不知道你可以有多个选择器。也许这会帮助别人。 $(this)[0]代表正在处理的选择器。尽管如此,仍然存在向拖放目标添加可拖动的问题。

     <script type ="text/javascript">
     var paraC = document.createElement("p");
     var paraW = document.createElement("p");
     var correct = document.createTextNode("Good Choice!");
     var wrong = document.createTextNode("Not Quite!");
     var dropElement;

    $(function() {
      $( "#yesDrag, #yesDrag1" ).draggable({
                                  drag: function(event, ui){
                                    paraC.appendChild(correct);
                                    $(this)[0].appendChild(paraC);
                                    dropElement.appendChild($(this)[0]);
                                  }
        });
        $( "#noDrag, #noDrag1" ).draggable({
                                 helper:"clone",
                                 revert: true,
                                 drag: function(event, ui){
                                    paraW.appendChild(wrong);
                                    $(this)[0].appendChild(paraW);
                                 }
        });
        $( "#droppable, #droppable1" ).droppable({
                                  drop:function( event, ui ) {
                                       dropElement = $(this)[0];
                                  }
        });

      });
      </script>