如何指定';x';和';y';通过JavaScript创建和附加的textNode的属性?
我需要指定动态生成的文本片段在网页中的确切位置。如何做到这一点?将文本放置在可以设置样式的内联元素中,然后在此元素上使用绝对定位如何指定';x';和';y';通过JavaScript创建和附加的textNode的属性?,javascript,html,web-applications,Javascript,Html,Web Applications,我需要指定动态生成的文本片段在网页中的确切位置。如何做到这一点?将文本放置在可以设置样式的内联元素中,然后在此元素上使用绝对定位 function placeText(x, y, text) { var span = document.createElement('span'); span.style.position = 'absolute'; span.style.top = y + 'px'; span.style.left = x + 'px';
function placeText(x, y, text) {
var span = document.createElement('span');
span.style.position = 'absolute';
span.style.top = y + 'px';
span.style.left = x + 'px';
span.appendChild(document.createTextNode(text));
document.body.appendChild(span);
return span;
}
placeText(200, 300, 'hello world');
将文本放置在可以设置样式的内联元素中,然后在此元素上使用绝对定位
function placeText(x, y, text) {
var span = document.createElement('span');
span.style.position = 'absolute';
span.style.top = y + 'px';
span.style.left = x + 'px';
span.appendChild(document.createTextNode(text));
document.body.appendChild(span);
return span;
}
placeText(200, 300, 'hello world');
文本节点没有属性,您必须先将它们包装到元素中,然后才能设置它们的样式。文本节点没有属性,您必须先将它们包装到元素中,然后才能设置它们的样式。