Internet Explorer 10 - (Javascript 或 jQuery) 如何在不知道属性和属性是什么类
Internet Explorer 10 - (Javascript or jQuery) How to change attributes and property without knowing which type it is (dynamic changes)?
请耐心等待伪代码和格式,我真的不记得我头顶上的代码。
在IE9及更低版本中,我知道您可以使用setAttribute((方法设置属性和属性,但是在IE10中似乎已通过分别分隔属性和属性进行了更改。
甚至做类似的事情
element.setAttribute("className", "myClass");
将元素设置为显示为
<tag className = "myClass" />
甚至没有设置属性。
在我的代码中,我有一个 JSON 列表,用于存储属性/属性的名称和值,我只是使用 setAttribute(( 设置了这些属性/属性;
createElement(tag, list)
{
//pseudocode
var element = createElementWithTag(tag);
for each(attr in attributes)
if (attributes.hasOwnKey(attr))
element.setAttribute(attr, attributes[attr]);
}
这在IE7-9上工作正常,但在IE10中完全失败。
有没有办法可以设置属性和属性,而无需哪种类型(attr 或 prop(?
我什至想不出一种动态设置属性的方法,而无需硬编码我要更改的属性的情况。
解决方案也可能有所帮助。
我认为这样的东西是你想要的:
function tag(tag, attribs) {
if(tag.charAt) tag = document.createElement(tag);
var alias = {
htmlFor: "for",
className: "class"
}, prop;
for (prop in attribs) if (attribs.hasOwnProperty(prop)) {
if("style" == prop) tag.style.cssText = attribs[prop];
try {
tag[prop] = attribs[prop];
} catch (h) {
tag.setAttribute( alias[prop] || prop, attribs[prop]);
}
}
return tag;
}
//test it:
tag("a", {href:"/", target:"_blank", innerHTML:" some text ", className: "1 2 3"}).outerHTML
在IE8和Chrome中测试。
如果有人知道更多的 dom->attrib 映射(如 htmlFor(,请将它们编辑到答案中,谢谢!
相关文章:
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 我可以'我似乎不知道如何修复javascript中的两个lint.有人能帮我理解吗
- 在不知道深度或父属性的情况下从对象中删除属性
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- Steam Web API:I'我不知道该如何处理这个看起来像哈希的url来生成图像
- Css order属性不't在Jquery设置时更新
- 使用.on动态添加jquery/js不知道的html元素
- 这个旋钮没有更新;我不知道为什么
- 我不知道为什么我的代码是错误的?又有什么错
- 如何在不知道关键字的情况下访问javascript对象值
- Internet Explorer 10 - (Javascript 或 jQuery) 如何在不知道属性和属性是什么类
- 当我不知道属性名称时,如何从 javascript 对象中获取值
- 如何访问预先不知道id的json属性
- 为什么Visual Studio 2013不知道我定义的属性的定义
- 访问JavaScript'的对象属性,而不知道该属性名称
- three.js;无法读取属性'位置'of undefined和don'我不知道为什么
- 如何获得JavaScript对象的所有属性值(不知道键)
- 当你不知道属性是什么的时候,有没有办法在javascript中干净地使用隐藏类?
- javascript的设计模式,当你不知道一个属性将是一个变量还是一个函数
- 在不知道名称的情况下访问对象中的对象的多个属性值