【发布时间】:2011-08-30 04:32:49
【问题描述】:
我想用下面的 JavaScript 制作一个按钮...
var button = document.createElement('button');
button.type = 'button';
button.appendChild(document.createTextNode('I am button'));
document.body.appendChild(button);
效果很好,IE7/8 除外(到目前为止我都测试过)。
留言:
Object doesn't support this action
线路:185
字符:9
代码:0
URI:http://example.com/widget.js
我找到了解决方法...
document.body.innerHTML = '<button type="button">I am button</button>';
即设置innerHTML,让浏览器进行解析。
有没有其他方法可以在不设置innerHTML 属性的情况下使其在 IE 中工作?
【问题讨论】:
-
这行得通吗:jsfiddle.net/PHrpz ? (我没有 IE)
-
@stewe 不,它在 IE 7 或 8 中不起作用(我使用了我的 Chrome IE 扩展程序,但它不起作用)
-
@Phil 我添加了错误消息。看来 IE必须预先知道
type属性,否则它有问题。 -
@alex 看看 Ray 回答中的文章,你可以试试
setAttribute() -
@Phil 我什至没想过要检查
setAttribute()。它似乎在 IE7 + 8 中工作。认为可能是时候将评论转换为答案了:)
标签: javascript button