用javascript函数创建链接 - innerHTML =“... HttpLink ...”

时间:2022-04-20 01:30:14

I'm learning to write with html, but I have one problem I cannot understand. The following code works in other browsers but not in IE9. I know it has something to do with innerHTML but I could not understand the answers I found for this.

我正在学习使用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>

What should I change WITHOUT changing the structure (only the innerHTMl-part if possible)?

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

2 个解决方案

#1


5  

Since you're creating an a element, you simply assign the href to the element via the .href property.

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

You can set its text content with .innerHTML as a convenience, though it's not really a "pure" approach.

您可以使用.innerHTML设置其文本内容,但这并非真正的“纯粹”方法。

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

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

document.body.appendChild( img11 );

Another way to set the text content would be like this...

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

img11.appendChild(document.createTextNode("Google"));

#2


3  

You code is creating another HTML page inside the original. That is wrong and invalid. It's because some browsers are forgiving and correcting things that your code even works.

您的代码正在原始内部创建另一个HTML页面。这是错误的,也是无效的。这是因为有些浏览器会宽容并纠正您的代码甚至可以正常运行的事情。

If you're creating a hyperlink element, you should be adding text or an image or other inline elements inside it using innerHTML.

如果要创建超链接元素,则应使用innerHTML在其中添加文本或图像或其他内联元素。

You should change its attribute to set the href with img11.href='http://xyz.com';

您应该更改其属性以使用img11.href ='http://xyz.com'设置href;

#1


5  

Since you're creating an a element, you simply assign the href to the element via the .href property.

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

You can set its text content with .innerHTML as a convenience, though it's not really a "pure" approach.

您可以使用.innerHTML设置其文本内容,但这并非真正的“纯粹”方法。

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

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

document.body.appendChild( img11 );

Another way to set the text content would be like this...

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

img11.appendChild(document.createTextNode("Google"));

#2


3  

You code is creating another HTML page inside the original. That is wrong and invalid. It's because some browsers are forgiving and correcting things that your code even works.

您的代码正在原始内部创建另一个HTML页面。这是错误的,也是无效的。这是因为有些浏览器会宽容并纠正您的代码甚至可以正常运行的事情。

If you're creating a hyperlink element, you should be adding text or an image or other inline elements inside it using innerHTML.

如果要创建超链接元素,则应使用innerHTML在其中添加文本或图像或其他内联元素。

You should change its attribute to set the href with img11.href='http://xyz.com';

您应该更改其属性以使用img11.href ='http://xyz.com'设置href;