在 IE 中,可以使用以下两种方式来创建一个元素:

1、document.createElement('table')

2、document.createElement('<table border="0">')

 

而在 Firefox 只支持:

document.createElement('table')

 

如果使用 document.createElement('tagName') 的方式来创建元素,那么在创建完元素后设置相关的属性,这个使用起来比较麻烦一点,而且代码也不怎么好看。

 

 为了解决这个问题,我自己写了一个方法:

 

kstar.createElement = function (tagName, properties) {
    
/// <summary>
    /// 调用 document.createElement 方法创建一个元素, 定义此方法是因为 IE 和 Firefox 之间有差别
    /// </summary>
    /// <param name="tagName" type="String">
    /// 标签名称
    /// </param>
    /// <param name="properties" type="Object">
    /// 标签的属性名,此属性名与 HTML 中设置的一样
    /// </param>

    
var el = document.createElement(tagName);
    
if (properties) {
        
for (var key in properties) {
            el.setAttribute(key, properties[key]);
        }
    }

    
return el;
}

相关文章:

  • 2021-09-28
  • 2022-12-23
  • 2021-08-08
  • 2021-12-17
  • 2021-08-10
猜你喜欢
  • 2021-09-23
  • 2021-08-30
  • 2021-06-01
  • 2022-12-23
  • 2021-12-05
  • 2021-07-23
相关资源
相似解决方案