Javascript 添加具有innerHTML属性的按钮

Javascript 添加具有innerHTML属性的按钮,javascript,html,Javascript,Html,我尝试在中添加html代码,因此我尝试使用以下方法: function editTextArea(element) { var options = document.getElementById("options"); options.innerHTML = options.innerHTML + "Cols: <input type='text' id='colsTextArea' maxlength='3' /><br>Rows: <input ty

我尝试在
中添加html代码,因此我尝试使用以下方法:

function editTextArea(element) {
   var options = document.getElementById("options");
   options.innerHTML = options.innerHTML + "Cols: <input type='text' id='colsTextArea' maxlength='3' /><br>Rows: <input type='text' id='rowsTextArea' maxlength='2' /><br><button type='button' onclick='updateTextArea('" + element.id + "')' >Add</button><br>";
}
函数编辑文本区域(元素){
var options=document.getElementById(“选项”);
options.innerHTML=options.innerHTML+“列:
行:
添加
”; }
但这就是我得到的

<button type="button" onclick="updateTextArea(" textarea0')'="">Agregar</button>
Agregar
我的问题是引号,所以我后来尝试使用createElement(“按钮”),但现在我无法添加onclick属性


我没有使用jQuery,因此如果没有它,就有一个解决方案会很好。

对updateTextArea的函数调用需要使用与onclick属性不同的引号。您不能执行onclick='alert('hi');',因为单引号终止onclick属性

function editTextArea(element) {
   var options = document.getElementById("options");
   options.innerHTML = options.innerHTML + "Cols: <input type='text' id='colsTextArea' maxlength='3' /><br>Rows: <input type='text' id='rowsTextArea' maxlength='2' /><br><button type='button' onclick='updateTextArea(" + '"' + + element.id + '"' + ")' >Add</button><br>";
}

如果使用第二个选项,则可以使用setAttribute()方法

var ele=document.createElement('button')

ele.setAttribute('onclick','method_name')

也可以通过转义引号来完成:

options.innerHTML = options.innerHTML + "Cols: <input type='text' id='colsTextArea' maxlength='3' /><br>Rows: <input type='text' id='rowsTextArea' maxlength='2' /><br><button type='button' onclick=\"updateTextArea(\'" + id + "\')\" >Add</button><br>";
options.innerHTML=options.innerHTML+“列:
添加
”;
在没有jQuery这样的库的情况下操作dom永远不会在所有浏览器上工作,除非您花费数小时进行优化,就像这个问题一样。“我打赌它会和jQuery一起工作。@Marc说真的,这超出了讽刺的程度:-)是吗?好的,我很抱歉:)你认为是否有可能在html中使用Angular这样做:在ts文件中:HTMLstring='a按钮';在HTML中
options.innerHTML = options.innerHTML + "Cols: <input type='text' id='colsTextArea' maxlength='3' /><br>Rows: <input type='text' id='rowsTextArea' maxlength='2' /><br><button type='button' onclick=\"updateTextArea(\'" + id + "\')\" >Add</button><br>";