本文介绍了JavaScript setAttribute替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个创建元素
的JS函数,该函数带有两个参数strName和objAttributes

I am creating this JS function that creates an elementThe function takes two parameters strName and objAttributes

function createElement( strName, objAttributes )
{
  var elem = document.createElement(strName);

  for ( var i in objAttributes )
    elem.setAttribute(i, objAttributes[i]);

  return elem;
}

在Fx上工作正常,但在MSIE
中效果不佳setAttibute方法有问题,建议的解决方法是

This works fine in Fx, but not in MSIEI know that the setAttibute method is buggy and the proposed workaround is

elem.attr = 'val';

但是现在我不知道如何在循环中编写它。

But right now I have no idea how to write this inside my loop.

我已经尝试了elem.style和elem ['style'],但都没有用。

I have tried both elem.style and elem['style'] but none of them works.

有人可以给我一些建议吗? ,

Can anyone give me some advice,

预先感谢

t

推荐答案

使用 elem [i]

function createElement( strName, objAttributes )
{
  var elem = document.createElement(strName);

  for ( var i in objAttributes )
      elem[i] = objAttributes[i];

  return elem;
}

这篇关于JavaScript setAttribute替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:21