<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div>
<p id="test"></p>
</div>
<script>
var newLink = document.createElement('a'); // #1
newLink.href = 'http://google.fr'; // #2
document.getElementById('test').appendChild(newLink); // #3
</script>
</body>
</html>
This is not working, and I can't understand why.
<a>
<a href="http://google.fr">
) test
and add this element to the <p>
tag which has test
as an ID. Why it is not working?
Try adding some content to the element as well by setting the .innerHTML
property. As it is now you are just adding an empty anchor. Try something like this:
var newLink = document.createElement('a');
newLink.href = 'http://google.fr';
newLink.innerHTML = 'My anchor';
document.getElementById('test').appendChild(newLink);
使用innerHTML
属性将您创建的标签附加到段落标签中。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.