나는 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;
스크립트가 html 파일과 정확히 같은 폴더에 있습니까? 하위 폴더가 없습니까? – Marc
예, 동일한 폴더에 있습니다. 요소 작업을하고 올바른 위치에 복사하려고했습니다. –
codepen에 추가 한 JS 코드는 "script.js"입니다. 맞습니까? codePen에서 작동하므로 폴더 구조에 문제가 있어야합니다.
태그의 스크립트 가져 오기는 필요하지 않으므로 제거하십시오. -> [내 코드 예제] (http://codepen.io/anon/pen/mErwLy). 폴더 구조를 알려주시겠습니까 – Marc