首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将内部属性添加到附加的标签元素(将数据从firebase数据库附加到表)?

如何将内部属性添加到附加的标签元素(将数据从firebase数据库附加到表)?
EN

Stack Overflow用户
提问于 2019-01-13 21:05:02
回答 1查看 26关注 0票数 0

在这里,我尝试更改附加的button元素的内部属性。

我尝试使用innerHtml属性。但是它没有给我预期的结果。

这是我的代码片段。

代码语言:javascript
复制
function userAction(){

  }

    function createElementWithText(tag, text) {
    var elm = document.createElement(tag);
    elm.textContent = text;
    return elm;
  }

    function createElementWithAttributeText(tag, text, attribute) {
    var elm = document.createElement(tag);
    elm.textContent = text;
    elm.innerHTML = attribute;
    return elm;
    }


   var tr = document.createElement('tr');
   var td = document.createElement('td');
   var button = document.createElement('button');



   tr.appendChild(createElementWithText('td', userName));
   tr.appendChild(createElementWithText('td', userEmail));
   tr.appendChild(createElementWithText('td', userPassword));
   tr.appendChild(createElementWithAttributeText('button', 'Reveiew', 'type="button" onclick="userAction()" class="btn btn-primary mt-4"'));

实际上,我的期望是<button type="button" onclick="userAction()" class="btn btn-primary mt-4">Review</button>

但结果是<button>type="button" onclick="userAction()" class="btn btn-primary mt-4"</button>

有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-13 21:23:14

也许像这样?

代码语言:javascript
复制
function userAction(){

  }

    function createElementWithText(tag, text) {
    var elm = document.createElement(tag);
    elm.textContent = text;
    return elm;
  }

    function createElementWithAttributeText(tag, text, attribute) {
    var elm = document.createElement(tag);
    elm.textContent = text;
    elm.innerHTML = attribute;
    return elm;
    }


   var tr = document.createElement('tr');
   var td = document.createElement('td');
   var button = document.createElement('button');



   tr.appendChild(createElementWithText('td', userName));
   tr.appendChild(createElementWithText('td', userEmail));
   tr.appendChild(createElementWithText('td', userPassword));
   var newbtn=document.createElement('button')
   newbtn.setAttribute('type','button')
   newbtn.setAttribute('onclick',userAction)
   newbtn.setAttribute('class','btn btn-primary mt-4')
   newbtn.innerHTML='Review'
   tr.appendChild(newbtn)

我希望这会对你有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54169152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档