如何通过javascript获得IE8中的非标准属性?
我有一个包含以下doctype的HTML页面:如何通过javascript获得IE8中的非标准属性?,javascript,html,internet-explorer-8,strict,Javascript,Html,Internet Explorer 8,Strict,我有一个包含以下doctype的HTML页面: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 在FF和Chrome中显示“blahblah”,但在IE8中显示null。另外,appletElement.attributes['src']未定义 有人知道如何在IE8中以严格模式获取src属性吗 谢谢 测试用例 var aplt=document.ge
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
在FF和Chrome中显示“blahblah”,但在IE8中显示null
。另外,appletElement.attributes['src']
未定义
有人知道如何在IE8中以严格模式获取src
属性吗
谢谢
测试用例
var aplt=document.getElementById('myapplet');
警报(aplt.getAttribute('src');
在IE8中对我有效。你试过了吗
appletElement.src
我找到了解决办法 我没有使用
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;
}
函数wrs\u createElement(元素名称、属性、创建者){
如果(属性===未定义){
属性={};
}
如果(创建者==未定义){
创建者=文档;
}
var元素;
/*
*Internet Explorer修复程序:
*如果动态创建新对象,则无法设置非标准属性。
*例如,不能在“applet”对象上设置“src”属性。
*其他浏览器将抛出异常并运行标准代码。
*/
试一试{
var html='';
element=creator.createElement(html);
}
捕获(e){
element=creator.createElement(elementName);
for(属性中的变量attributeName){
setAttribute(attributeName,attributeName[attributeName]);
}
}
返回元素;
}
好的,我的问题没有诚意。小程序对象不插入HTML,而是使用document.createElement动态创建的。为该小程序设置非标准属性时,以后将无法检索它。它返回null
ever。
<!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>
appletElement.src
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;
}