如何让我的脚本工作/响应

时间:2016-07-07 08:39:28

标签: javascript

我在PC上并没有使用localhost或任何东西,只是制作了一个.html并开始尝试。我已经将脚本链接到了#34; src"并且通过" href",因为我下载了它们,但它们仍然无法正常工作。我继续http://codepen.io/anon/pen/yJogzz并且工作正常。

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
        <script type="text/javascript" src="script.js"></script>

        <link type="text/css"href="./Style.css" rel="stylesheet">
        <link type="text/javascript"href="./Style.js" rel="stylesheet">
    </head>

    <body>
    <span class="about" id="but01">Click on me 1</span>
    <span class="about" id="but02">Click on me 2</span>

    <div id="s1" class="hidden" id="draggable-element">
        <div id="draggable-element">Text here</div>
    </div>

    <div id="s2" class="hidden">2</div>
    </body>
</html>

CSS:

body{
    background-color: #101010;
    color: #ff9900;
}

.hidden{
    display:none;
}

[draggable=true] {
    cursor: move;
}
#draggable-element {
  width:100px;
  height:100px;
  background-color:#ff9900;
  color:white;
  padding:10px 12px;
  cursor:move;
  position:relative; /* important (all position that's not `static`) */
}

SCRIPT:

// Makes div dissapear on click on div
    $('span[id^="but0"]').click(function(){
            var id = $(this).attr('id').substr(4);
            if($('#s' + id).is(':visible'))
                 $('#s' + id).hide();
            else
                $('#s' + id).show();
    });



var selected = null, // Object of the element to be moved
    x_pos = 0, y_pos = 0, // Stores x & y coordinates of the mouse pointer
    x_elem = 0, y_elem = 0; // Stores top, left values (edge) of the element

// Will be called when user starts dragging an element
function _drag_init(elem) {
    // Store the object of the element which needs to be moved
    selected = elem;
    x_elem = x_pos - selected.offsetLeft;
    y_elem = y_pos - selected.offsetTop;
}

// Will be called when user dragging an element
function _move_elem(e) {
    x_pos = document.all ? window.event.clientX : e.pageX;
    y_pos = document.all ? window.event.clientY : e.pageY;
    if (selected !== null) {
        selected.style.left = (x_pos - x_elem) + 'px';
        selected.style.top = (y_pos - y_elem) + 'px';
    }
}

// Destroy the object when we are done
function _destroy() {
    selected = null;
}

// Bind the functions...
document.getElementById('draggable-element').onmousedown = function () {
    _drag_init(this);
    return false;
};

document.onmousemove = _move_elem;
document.onmouseup = _destroy;

2 个答案:

答案 0 :(得分:1)

尝试导入<body>标记底部的脚本。

当你使用jQuery时,我建议将你的代码包装在$(document).ready()函数内(不需要)

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
    <script type="text/javascript" src="script.js"></script>

    <link type="text/css"href="./Style.css" rel="stylesheet">
</head>

<body>
<span class="about" id="but01">Click on me 1</span>
<span class="about" id="but02">Click on me 2</span>

<div id="s1" class="hidden" id="draggable-element">
    <div id="draggable-element">Text here</div>
</div>

<div id="s2" class="hidden">2</div>
    <script type="text/javascript" src="script.js"></script>
</body>

这段代码对我有用。你绝对不需要javascript服务器

答案 1 :(得分:0)

您可以使用Python的简单HTTP服务器来托管文件。脚本标记不会加载,因为文件系统不会响应GET请求。

确保安装了Python 2.x,然后运行:

python -m SimpleHTTPServer

在终端中,从目录中保存文件:

cd /path/to/files

然后您就可以访问以下文件:

http://localhost:8000/

或者,您可以启动Chrome以允许访问本地文件:

chrome --allow-file-access-from-files

了解更多here

如果你想避免这些选择。将JS内联放在脚本标记内的HTML文档中。就像这个CodePen