Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在页面中心有一个带有文本的 div。当我单击 div 时,它应该移动到页面上的任何位置(例如:100px 以上)。我该怎么做?
通过 id 检索元素,然后设置onclick handler. 然后你可以position为元素设置如下:
onclick
handler
position
jsFiddle
var element = document.getElementById('myDivId'); element.onclick = function(){ element.style.position = "absolute"; element.style.left = '100px'; element.style.top = '100px'; }