2

我有一个具有此文档类型的 HTML 页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

但是,HTML 包含此标记:

<applet src="blahblah"></applet>

(编辑:实际上 HTML 不包含小程序。小程序是由其他 javascript 代码动态创建的)。

是的,我知道它applet已被弃用,并且我知道applet标签不能包含src属性,但我无法编辑该 HTML 代码。

问题是这个Javascript代码:

alert(appletElement.getAttribute('src'));

在 FF 和 Chrome 中显示“blahblah”,但在 IE8 中显示null. 另外,appletElement.attributes['src']没有定义。

有人知道如何src在严格模式下获取 IE8 中的属性吗?

谢谢

4

3 回答 3

3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
                               "http://www.w3.org/TR/html4/strict.dtd">
<title>Test Case</title>
<applet id="myapplet" src="blahblah"></applet> 
<script>
var aplt = document.getElementById('myapplet');
alert(aplt.getAttribute('src'));
</script>

在 IE8 中为我工作。

于 2010-10-06T13:01:59.250 回答
0

你有没有尝试过

appletElement.src
于 2010-10-06T13:28:54.097 回答
0

我找到了解决方案。

document.createElement我没有使用动态创建这些小程序,而是使用以下函数:

function wrs_createElement(elementName, attributes, creator) {
    if (attributes === undefined) {
        attributes = {};
    }

    if (creator === undefined) {
        creator = document;
    }

    var element;

    /*
     * Internet Explorer fix:
     * If you create a new object dynamically, you can't set a non-standard attribute.
     * For example, you can't set the "src" attribute on an "applet" object.
     * Other browsers will throw an exception and will run the standard code.
     */

    try {
        var html = '<' + elementName + ' ';

        for (var attributeName in attributes) {
            html += attributeName + '="' + attributes[attributeName].split('"').join('\\"') + '" ';
        }

        html += '>';
        element = creator.createElement(html);
    }
    catch (e) {
        element = creator.createElement(elementName);

        for (var attributeName in attributes) {
            element.setAttribute(attributeName, attributes[attributeName]);
        }
    }

    return element;
}
于 2010-10-07T10:49:20.380 回答