简体   繁体   English

如何使用JavaScript在页面中的特定坐标处放置字符

[英]How do I position a character at a specific coordinate in a page with JavaScript

I tried this code: 我尝试了这段代码:

var span = document.createElement('span')
span.style.position = 'absolute'
span.style.color = 'red'
span.style.left = 100
span.style.top = 100
span.innerHTML = '*'
document.body.insertBefore(span, document.body.firstChild)

But this code still positions the asterisk at coordinate (0, 0) instead of (100, 100). 但是此代码仍将星号定位在坐标(0,0)而非(100,100)处。 Here is a jsfiddle URL: http://jsfiddle.net/E5NDV/ 这是一个jsfiddle URL: http : //jsfiddle.net/E5NDV/

Could you please help me in getting this right? 您能帮我解决这个问题吗?

var span = document.createElement('span')
span.style.position = 'absolute'
span.style.color = 'red'
span.style.left = "100px"
span.style.top = "100px"
span.innerHTML = '*'
document.body.insertBefore(span, document.body.firstChild)

http://jsfiddle.net/E5NDV/1/ http://jsfiddle.net/E5NDV/1/

The values for left and top should be a String giving a number and a unit, eg px for pixels , ie '100px' . lefttop的值应该是一个给出数字和单位的String ,例如px表示像素 ,即'100px' Which child the node is appended as does not matter so much for absolute positioning as this information is not used to decide where to position it, using appendChild would work just as well as insertBefore . 对于绝对定位,将节点附加到哪个子节点并没有太大关系,因为该信息不会用于确定将节点放置在何处,使用appendChild会和insertBefore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM