2016-07-07 1 views
1

나는 PC에 있지만 localhost 또는 기타를 사용하지 않고 방금 .html을 만들고 시도하기 시작했습니다. 나는 그들을 다운로드했기 때문에 "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; 

답변

1

<body> 태그의 하단에 스크립트를 가져보십시오.

그리고 당신은 내가 $(document).ready() 함수 내에서 코드를 포장 권장, jQuery를 사용하고있는로 (필요하지)

<!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> 

이 코드는 나를 위해 작동합니다. 자바 스크립트 용 서버가 필요 없습니다.

+0

스크립트가 html 파일과 정확히 같은 폴더에 있습니까? 하위 폴더가 없습니까? – Marc

+0

예, 동일한 폴더에 있습니다. 요소 작업을하고 올바른 위치에 복사하려고했습니다. –

+0

codepen에 추가 한 JS 코드는 "script.js"입니다. 맞습니까? codePen에서 작동하므로 폴더 구조에 문제가 있어야합니다. 태그의 스크립트 가져 오기는 필요하지 않으므로 제거하십시오. -> [내 코드 예제] (http://codepen.io/anon/pen/mErwLy). 폴더 구조를 알려주시겠습니까 – Marc

0

파이썬의 간단한 HTTP 서버를 사용하여 파일을 호스팅 할 수 있습니다. 스크립트 태그는 파일 시스템이 GET 요청에 응답하지 않을 때로드되지 않습니다.

파이썬 2.x를 설치했는지 확인하고 실행

python -m SimpleHTTPServer 

을 터미널에서 디렉토리에 파일이 저장됩니다에서 :

cd /path/to/files 

그런 다음에 액세스 할 수 있습니다 다음 위치의 파일 :

또는 Chrome을 실행하여 로컬 파일에 대한 액세스를 허용 할 수 있습니다 :

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

자세히 알아보기 here.

이러한 옵션을 사용하지 않으려는 경우. 스크립트 태그 내의 HTML 문서에 JS 인라인을 넣습니다. 이 CodePen처럼.

관련 문제