3

I am facing a problem where I want a line break
tag added dynamically in the code never works after the first tag which is anchor I want every link added dynamically should go on new line

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script>
function addAnchorNode(){
    var link = document.createElement('a');
    link.setAttribute('href', 'http://Google.co.uk');
    link.innerHTML = "Hello, Google!";

    document.body.appendChild(link);
    document.body.appendchild(document.createElement('br')); //Never Works
}
</script>
</head>

<body>
<button onclick="addAnchorNode()">Click me</button>
</body>
</html>
4

2 回答 2

3

你必须在你的 BR 之后有一些东西,并且没有错字(“appendChild”,而不是“appendchild”)。

这有效:

function addAnchorNode(){
    var link = document.createElement('a');
    link.setAttribute('href', 'http://Google.co.uk');
    link.innerHTML = "Hello, Google!";

    document.body.appendChild(document.createElement('br'));
    document.body.appendChild(link);
}

示范

于 2012-10-13T10:46:10.823 回答
0

上面的代码是正确的,问题是“appendChild”,请在下面找到工作代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script>
function addAnchorNode(){
    var link = document.createElement('a');
    link.setAttribute('href', 'http://Google.co.uk');
    link.innerHTML = "Hello, Google!";

    document.body.appendChild(link);
    document.body.appendChild(document.createElement("br"));
    //document.body.appendchild(document.createElement('br')); //Never Works
}
</script>
</head>

<body>
<button onclick="addAnchorNode()">Click me</button>
</body>
</html>
于 2017-03-21T10:51:46.280 回答