【问题标题】:JSFiddle to HTMLJSFiddle 到 HTML
【发布时间】:2014-11-10 04:45:32
【问题描述】:

我是 javascript 和 jquery 的新手。我有一个用于滑动条的代码,它在 jsfiddle 上运行良好;但是,当我尝试将其作为 HTML 文件运行时,它不起作用。我不知道我在这里错过了什么,或者需要做什么才能使这段代码正常工作。请让我知道我必须在代码中包含哪些库文件才能使条形图在 HTML 中滑动。

我在 jsfiddle 上的代码链接是:http://jsfiddle.net/sgx9L8sx/

任何帮助将不胜感激。

提前致谢。

`.box {
width:40px;
height:10px;
background:blue;
}
.box1 {
width:25px;
height:10px;
position:relative;
top:40px;
left:40px;
background:black;
}
.result{
position:fixed;
top:20px;  
}
.result1{
position:fixed;
top:70px;
}

<script>    
$(".box").draggable({
axis: "x",
drag: function(event, ui) {
    var y2 = ui.position.top;
    var x2 = ui.position.left;


    if (reverting){
        event.preventDefault();
        event.stopImmediatePropagation();
    } else if (x2 > 100) {
        reverting = true;
        revertDrag($('.box'));
    }
    else if (x2<0) {

    }
    }
 });

 function revertDrag(element){
 element.draggable('disable');
 element.animate({
    top: 0,
  }, {
    duration: 500,

    complete: function() {
        reverting = true;
        element.draggable('enable');


    }
})

}
$(".box1").draggable({
axis: "x",
drag: function(event, ui) {
    var y3 = ui.position.top;
    var x3 = ui.position.left;




    if (reverting1){
        event.preventDefault();
        event.stopImmediatePropagation();
    } else if (x3 > 200) {
        reverting1 = true;
        revertDrag1($('.box1'));
        alert("this is incorrect");
    }
    else if (x3<40) {
    alert("Wrong submission");
        revertDrag2($('.box1'));
       }
 }
 });

function revertDrag1(element){
element.draggable('disable');
element.animate({
    top:40,
}, {
    duration: 500,

    complete: function() {
        reverting1 = false;
        element.draggable('enable');


    }
})

}
    function revertDrag2(element){
element.draggable('disable');
element.animate({
    top:40,
    left:70,
}, {
    duration: 500,

    complete: function() {
        reverting1 = true;
        element.draggable('enable');


    }
})

}
</script>

<body>
<div class="box">
</div>
<div class="result">
</div>
<div class="box1">

<div class="result1">
</div>
</div>
</body>

【问题讨论】:

  • 您是使用 HTML 页面的样式表还是使用页内样式?错误地阅读了这个问题:/我的错
  • 将样式包装在样式标签中
  • 您上面粘贴的代码是您放入 HTML 文件中的代码吗?如果是这样,那就有很多错误了……缺少、
  • 要调试此类错误,请转到浏览器的检查器,并检查控制台选项卡,它会为您提供一些不顺利的帮助。
  • 这只是一个草稿。我没有在这里添加 head 和 style 标签。但是在主 html 文件中添加了它们。

标签: javascript jquery html css


【解决方案1】:

您需要在代码中添加 jquery 引用

<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

【讨论】:

  • 谢谢 Balder :) 我找到了问题。
【解决方案2】:

如果您在小提琴中查看Framework and Extensions,您会看到您的基础库是jQuery 1.10.1,然后您在其后包含jQueryUI 1.10.3,这是一个JS 和一个CSS 文件。

可以在任何时候包含 CSS,但最好是在您的 JS 之前。 JS 顺序很重要,因为 jQuery 在 jQueryUI 之前包含。

所以在你的 HTML 中是这样的:

<script type='text/javascript' src='//code.jquery.com/jquery-1.10.1.js'></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js"></script>

【讨论】:

    【解决方案3】:

    您可以在以下网址找到您的演示页面(按 CTRL+U 查看源代码):

    http://fiddle.jshell.net/sgx9L8sx/show/

    例如,这是在标题上设置的:

    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <title> - jsFiddle demo</title>
        <script type='text/javascript' src='//code.jquery.com/jquery-1.10.1.js'></script>
        <link rel="stylesheet" type="text/css" href="/css/result-light.css">
        <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
        <script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js"></script>
        <style type='text/css'>
            .box {
            width:20px;
            height:10px;
            background:blue;
            }
            .box1 {
            width:20px;
            height:10px;
            position:relative;
            top:40px;
            background:black;
            }
            .result {
            position:fixed;
            top:20px;
            }
            .result1 {
            position:fixed;
            top:70px;
            }
        </style>
        <script type='text/javascript'>//<![CDATA[ 
            $(window).load(function(){
            });//]]>
        </script>
    </head>
    

    【讨论】:

    • 我知道这一点,但如果我在我的代码中使用它,它就不起作用。我必须下载整个库,然后它才能工作。而且我从不理解对结果灯文件的引用。这有什么意义?
    【解决方案4】:

    这是我将 fiddle 转换为 HTML 的方法。

    在 Windows 10 上的 Live Server 中运行时,生成的 HTML 页面可以正常工作

    我相信下面的 HTML 代码是不言自明的。 请阅读 cmets。

    起始代码来自 2 个来源:

    1. 适用于 HTML 5 的 vscode sn-p。它非常简单:html、head、body。
    2. The source for my fiddle

    这是我的 HTML。

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Construct HTML from FIDDLE</title>
        <!-- N.B. 
        see [my SO answer at](https://stackoverflow.com/questions/25868691/jsfiddle-to-html/68913137#68913137) 
        1. There are 4 sections total added to a vscode HTML 5 blank page. 
          - Each has BEGIN, END 
        2. The Source jsfiddle for my fork url is https://jsfiddle.net/JoeCodeswell/7mwuepL
          - the source for my fork url is         http://jsfiddle.net/KPCh4/4/
            - from [Gabriele Petrioli answer](https://stackoverflow.com/a/22697152/601770)
      -->
        <!-- 1.BEGIN FIDDLE STYLE BEGIN -->
        <style>
          .row {
            border: 1px solid red;
          }
        </style>
        <!-- 1.END FIDDLE STYLE END -->
    
        <!-- 2.BEGIN ADD FIDDLE RESOURCES >> BEGIN -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
        <!-- 2.END ADD FIDDLE RESOURCES >> END -->
      </head>
      <body>
        <!-- 3.BEGIN https://jsfiddle.net/JoeCodeswell/7mwuepLs/2/  >> HTML >> BEGIN -->
        <div id="DocumentResults"></div>
        <script id="document-template" type="text/x-handlebars-template">
          <div>
            {{#each this}}
              <div class="row">
                <div class="col-md-12">
                  <h2>{{Category}}</h2>
                  {{#DocumentList}}
                    <p>{{DocumentName}} at {{DocumentLocation}}</p>
                  {{/DocumentList}}
                </div>
              </div>
            {{/each}}
    
            <p>There are no documents available at this time</p>
    
          </div>
        </script>
        <!-- 3.END https://jsfiddle.net/JoeCodeswell/7mwuepLs/2/  >> HTML >> END -->
    
        <!-- 4.BEGIN https://jsfiddle.net/JoeCodeswell/7mwuepLs/2/  >> JavaScript + jQuery 2.0.2 >> BEGIN -->
    
        <script>
          $(function () {
            var source = $("#document-template").html();
            var template = Handlebars.compile(source);
            var html = template(data);
            $("#DocumentResults").html(html);
          });
    
          var data = [
            {
              Category: "General",
              DocumentList: [
                {
                  DocumentName: "Document Name 1 - Joe",
                  DocumentLocation: "Document Location 1 - Joe Bla",
                },
                {
                  DocumentName: "Document Name 2 - General",
                  DocumentLocation: "Document Location 2 - General",
                },
              ],
            },
            {
              Category: "Unit Documents",
              DocumentList: [
                {
                  DocumentName: "Document Name 1 - Unit Documents",
                  DocumentList: "Document Location 1 - Unit Documents",
                },
              ],
            },
            {
              Category: "Minutes",
            },
          ];
        </script>
        <!-- 4.END https://jsfiddle.net/JoeCodeswell/7mwuepLs/2/  >> JavaScript + jQuery 2.0.2 >> END -->
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      相关资源
      最近更新 更多