使用--- innerHTML ="... <a href='http://...'> HttpLink </a>在javascript函数中创建链接......"

Red*_*iet 3 javascript internet-explorer function innerhtml

我正在学习用html编写,但我有一个我无法理解的问题.以下代码适用于其他浏览器,但不适用于IE9.我知道它与某些事情有关,innerHTML但我无法理解我为此找到的答案.

<html> <head> <script type="text/javascript">
function hw_function1() {
    var img11=document.createElement("a"); 
    img11.innerHTML = "<html> <body> <a href='http://google.de'>Google</a> </body></html>";
    document.body.appendChild( img11 );
}
</script>
<body>
    <a href="#" onclick="javascript:hw_function1()";>Test</a>
</body> </html>
Run Code Online (Sandbox Code Playgroud)

在不改变结构的情况下我应该改变什么(如果可能的话,只有innerHTMl-part)?

小智 5

由于您正在创建a元素,因此只需通过.href属性将href指定给元素.

您可以将其文本内容设置.innerHTML为方便,但它并不是一种"纯粹"的方法.

var img11=document.createElement("a");

img11.href='http://google.de';
img11.innerHTML = 'Google';

document.body.appendChild( img11 );
Run Code Online (Sandbox Code Playgroud)

设置文本内容的另一种方法是这样的......

img11.appendChild(document.createTextNode("Google"));
Run Code Online (Sandbox Code Playgroud)