在这里,我尝试更改附加的button元素的内部属性。
我尝试使用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>。
有人能帮我解决这个问题吗?
发布于 2019-01-13 21:23:14
也许像这样?
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)我希望这会对你有所帮助!
https://stackoverflow.com/questions/54169152
复制相似问题