0

我目前正在尝试将此按钮放入我的 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>

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

4

3 回答 3

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 分开,并为点击事件使用事件侦听器。

这是你可以做到的。

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>

于 2015-07-01T15:37:15.650 回答
0

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

于 2015-07-01T14:54:12.990 回答
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>

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>";

}
table, td {
    border: 1px solid black;
}
<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>

于 2015-07-01T15:24:15.893 回答