我創建2組簡單的元素:爲什麼ClassName打破這個代碼?
var mydragg = function() {
return {
move: function(div, xpos) {
div.style.left = xpos + 'px';
},
startMoving: function(div, container, evt) {
evt = evt || window.event;
var posX = evt.clientX,
divLeft = div.style.left,
eWi = parseInt(div.style.width = "200px"),
cWi = parseInt(document.getElementById(container).style.width = "200px");
document.getElementById(container).style.cursor = 'move';
divLeft = divLeft.replace('px', '');
var diffX = posX - divLeft;
document.onmousemove = function(evt) {
evt = evt || window.event;
var posX = evt.clientX,
aX = posX - diffX;
if (aX < 0) aX = 0;
if (aX > cWi) aX = cWi;
mydragg.move(div, aX);
}
},
stopMoving: function(container) {
var a = document.createElement('script');
document.getElementById(container).style.cursor = 'default';
document.onmousemove = function() {}
},
}
}();
<div class="wrapper">
<div id="container">
<div class="green dragme" onmousedown='mydragg.startMoving(this,"container",event);' onmouseup='mydragg.stopMoving("container");'>
</div>
</div>
</div>
<div class="wrapper wrapper-red">
<div id="container">
<div class="red dragme" onmousedown='mydragg.startMoving(this,"container",event);' onmouseup='mydragg.stopMoving("container");'>
</div>
</div>
</div>
一切正常(綠色和紅色塊是通過鼠標拖動 - https://codepen.io/rinatoptimus/pen/GvMNPW),但不應該有多個ID與同名稱。所以,我已經改變了IDS(「容器」)上課,之後碼不起作用:https://codepen.io/rinatoptimus/pen/OjxWJO?editors=1010
哪裏是你改變/修改的代碼,「不工作」? – CBroe
https://codepen.io/rinatoptimus/pen/OjxWJO?editors=1010 – rinatoptimus