我正在学习用 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>
我应该在不改变结构的情况下改变什么(如果可能,只改变 innerHTML 部分)?
最佳答案
由于您正在创建一个 a
元素,您只需通过 .href
属性将 href 分配给该元素即可。
为方便起见,您可以使用 .innerHTML
设置其文本内容,尽管这并不是真正的“纯粹”方法。
var img11=document.createElement("a");
img11.href='http://google.de';
img11.innerHTML = 'Google';
document.body.appendChild( img11 );
另一种设置文本内容的方法是这样的......
img11.appendChild(document.createTextNode("Google"));
关于javascript - 使用 --- innerHTML = "...<a href=' http ://. 在 javascript 函数中创建链接。 .'>HttpLink</a>...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11588460/