Как добавить внутренние атрибуты в элемент добавленного тега (добавленные данные в таблицу из базы данных Firebase)? - PullRequest
0 голосов
/ 13 января 2019

Здесь я пытаюсь изменить внутренние атрибуты добавленного элемента кнопки.

Я пробую это свойство innerHtml. Но это не дает мне ожидаемого результата.

Вот мой фрагмент кода.

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>.

Может ли кто-нибудь помочь мне с этой проблемой?

1 Ответ

0 голосов
/ 13 января 2019

Может, вот так?

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=createElement('button')
   newbtn.setAttribute('type','button')
   newbtn.setAttribute('onclick',userAction)
   newbtn.setAttribute('class','btn btn-primary mt-4')
   newbtn.innerHTML='Review'
   tr.appendChild(newbtn)

Надеюсь, это вам поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...