动态标签插入在 Windows IE 或 Firefox 上不起作用

发布于 2024-11-05 00:22:03 字数 590 浏览 2 评论 0原文

我使用以下策略动态地将 html 标签插入到文档中。它适用于 Mac Chrome 和 Firefox,但不适用于 Windows IE 或 Windows Firefox。我怀疑这与分配给新创建的 div 标签的innerHTML 属性的字符串中的尖括号有关。有什么想法吗?

function addElement() {

      var ni = document.getElementById('stateSet');
      var newdiv = document.createElement('div');
      var divIdName = 'state'+ stateCount;
      stateCount++;
      newdiv.setAttribute('id',divIdName);
      newdiv.innerHTML = '<input id="name" type="text"/> <input id="setting" type="text"/><button type="button" >x</button>'
      ni.appendChild(newdiv);

    }

I am dynamically inserting html tags into the document using the following strategy. It works on Mac Chrome and Firefox but not on Windows IE or Windows Firefox. I suspect this has something to do with the angle brackets in the string assigned to the innerHTML property of the newly created div tag. Any ideas?

function addElement() {

      var ni = document.getElementById('stateSet');
      var newdiv = document.createElement('div');
      var divIdName = 'state'+ stateCount;
      stateCount++;
      newdiv.setAttribute('id',divIdName);
      newdiv.innerHTML = '<input id="name" type="text"/> <input id="setting" type="text"/><button type="button" >x</button>'
      ni.appendChild(newdiv);

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2024-11-12 00:22:03

尝试声明 statecount

function addElement() {
  var stateCount = 0;
  var ni = document.getElementById('stateSet');
  var newdiv = document.createElement('div');
  var divIdName = 'state'+ stateCount;
  stateCount++;
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = '<input id="name" type="text"/> <input id="setting" type="text"/><button type="button" >x</button>'
  ni.appendChild(newdiv);

}

http://jsfiddle.net/TPx3v/

Try declaring statecount

function addElement() {
  var stateCount = 0;
  var ni = document.getElementById('stateSet');
  var newdiv = document.createElement('div');
  var divIdName = 'state'+ stateCount;
  stateCount++;
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = '<input id="name" type="text"/> <input id="setting" type="text"/><button type="button" >x</button>'
  ni.appendChild(newdiv);

}

http://jsfiddle.net/TPx3v/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文