1

我有以下功能需要打印以给定点(x,y )为中心的动态生成的文本( txt

function printText(x,y,txt) {   
  var s = "";   
  s = "<b style=???>"+txt+"</b>";    
  document.getElementById("some_div").innerHTML = s;
}

我怎样才能做到这一点?

4

1 回答 1

1

制作positiondivrelative并在内部元素上使用absolute定位,如下所示:

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <title>Test</title>
        <meta charset="UTF-8" />
        <style>
            #some_div {
                postion:relative;
                height:300px;
            }
            #some_div b {
                position:absolute;
            }
        </style>
        <script>
            function printText(x, y, txt) {   
                var s = "<b style=\"left:" + x + "px;top:" + y + "px;\">"+txt+"</b>";    
                document.getElementById("some_div").innerHTML = s;
            }
        </script>
    </head>
    <body>
        <div id="some_div"></div>
        <button onclick="printText(100, 40, 'This is a test');">Click Me</button>
    </body>
</html>
于 2013-05-30T19:32:20.397 回答