将按钮插入HTML单元格

时间:2015-07-01 14:49:38

标签: html html-table htmlbutton

我目前正在尝试将此按钮添加到我的HTML表格中。

这是我要插入的代码:

<button class=\"btn btn-primary btn-xs my-xs-btn\" type='button' onClick='changeRec(".$num.")' >
<span class=\"glyphicon glyphicon-pencil\"></span> Edit
</button>

这是我尝试插入它。

<button type="button" class="btn btn-primary btn-lg" onclick="myFunction()">Add</button>

<script>
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cellInstruction = row.insertCell(-1);
cellInstruction.innerHTML = '<button class=\"btn btn-primary btn-xs my-xs-btn\" type='button' onClick='changeRec(".$num.")' >
                            <span class=\"glyphicon glyphicon-pencil\"></span> Edit
                        </button>';
}
</script>

任何帮助将不胜感激。谢谢!

3 个答案:

答案 0 :(得分:2)

您没有正确连接字符串,也不需要转义双引号。

这是工作示例。

<button type="button" class="btn btn-primary btn-lg" onclick="myFunction()">Add</button>
<table id="myTable"></table>
<script>
  function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(-1);
    var cellInstruction = row.insertCell(-1);
    cellInstruction.innerHTML = '<button class="btn btn-primary btn-xs my-xs-btn" type="button" onClick="changeRec(".$num.")" >'
    + '<span class="glyphicon glyphicon-pencil"></span> Edit</button>';
  }
</script>

但您应该将您的javascript与html分开,并为click事件使用eventlistener。

以下是你如何做到这一点。

var addBtn = document.getElementById('add-btn');

function addEditBtn() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(-1);
    var cellInstruction = row.insertCell(-1);
    cellInstruction.innerHTML = '<button class="btn btn-primary btn-xs my-xs-btn" type="button">'
    + '<span class="glyphicon glyphicon-pencil"></span> Edit</button>';
}

addBtn.addEventListener('click', addEditBtn, false);
<button type="button" class="btn btn-primary btn-lg" id="add-btn">Add</button>
<table id="myTable"></table>

另一种方法是使用document.createElement创建按钮元素。

var addBtn = document.getElementById('add-btn');

function addEditBtn() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(-1);
    var cellInstruction = row.insertCell(-1);
    var button = document.createElement('button');
    var span = document.createElement('span');
    button.setAttribute('class', 'btn btn-primary btn-xs my-xs-btn');
    button.setAttribute('type', 'button');
    span.setAttribute('class', 'glyphicon glyphicon-pencil');
    span.innerHTML = " Edit";
    button.appendChild(span);
    cellInstruction.appendChild(button);
}

addBtn.addEventListener('click', addEditBtn, false);
<button type="button" class="btn btn-primary btn-lg" id="add-btn">Add</button>
<table id="myTable"></table>

答案 1 :(得分:0)

innerHTML只获取/设置不包含标签本身的元素的值

答案 2 :(得分:0)

我想,你在括号中的某个地方搞砸了。我使用PHP测试了下面的代码,它运行正常:

<?php
    $num = 4;
    $insert = "<button class='btn btn-primary btn-xs my-xs-btn' type='button' onClick='changeRec(".$num.")'><span class='glyphicon glyphicon-pencil'></span> Edit</button>";
?>

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<table id="myTable">
  <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
  </tr>
  <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
  </tr>
  <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
  </tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(-1);
    var cell1 = row.insertCell(-1);

    cell1.innerHTML = "<?php echo $insert; ?>";
}
</script>

</body>
</html>

&#13;
&#13;
function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(-1);
    var cell1 = row.insertCell(-1);

    cell1.innerHTML = "<button class='btn btn-primary btn-xs my-xs-btn' type='button' onClick='changeRec()' ><span class='glyphicon glyphicon-pencil'></span> Edit</button>";

}
&#13;
table, td {
    border: 1px solid black;
}
&#13;
<body>

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
  <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
  </tr>
  <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
  </tr>
  <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
  </tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>
&#13;
&#13;
&#13;

相关问题