谁能告诉我为什么这个 javascript 函数调用不起作用?
这是一个 jsFiddle
JavaScript:
function createTable(tbody, rows, cols) {
if (tbody == null || tbody.length < 1) return;
for (var r = 1; r <= rows; r++) {
var trow = $("<tr>");
for (var c = 1; c <= cols; c++) {
$("<td>")
.text("Table")
//.createElement("div")
.appendTo(trow);
}
trow.appendTo(tbody);
}
}
$(document).ready(function() {
createTable($("#table"), 4, 4);
});
HTML:
<table id="table" border="1">
<tbody>
</tbody>
</table>
----------------------------
<table id="table1" border="1">
<tbody>
</tbody>
</table>
<br><br>
Select table size?
<form>
<input type="button" value="4X4" onclick="createTable('table1', 4, 4)">
</form>
JavaScript 调用的 JavaScript 函数工作正常(表),但 OnClick 调用的 JavaScript 函数不起作用(表1)。你能告诉我为什么吗?
还有人知道为什么 createElement("div")
也不起作用吗?
Here's a jsFiddle
JavaScript:
function createTable(tbody, rows, cols) {
if (tbody == null || tbody.length < 1) return;
for (var r = 1; r <= rows; r++) {
var trow = $("<tr>");
for (var c = 1; c <= cols; c++) {
$("<td>")
.text("Table")
//.createElement("div")
.appendTo(trow);
}
trow.appendTo(tbody);
}
}
$(document).ready(function() {
createTable($("#table"), 4, 4);
});
HTML:
<table id="table" border="1">
<tbody>
</tbody>
</table>
----------------------------
<table id="table1" border="1">
<tbody>
</tbody>
</table>
<br><br>
Select table size?
<form>
<input type="button" value="4X4" onclick="createTable('table1', 4, 4)">
</form>
The JavaScript function called by JavaScript works fine(table), but the JavaScript function called by the OnClick isn't working(table1). Can you tell me why?
Also does anyone know why the createElement("div")
isn't working either?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
修复了 http://jsfiddle.net/7WD8v/6/
您需要在头部加载脚本让它可用于该输入
Fixed http://jsfiddle.net/7WD8v/6/
You needed to load you script in the head to have it available for that input
http://jsfiddle.net/7WD8v/12/
http://jsfiddle.net/7WD8v/12/
您必须在小提琴中使用
no wrap
,并且 jQuery 没有createElement
方法(因为您将其作为 jQuery 方法调用)。您可以使用 .append()小提琴
You have to use
no wrap
in the fiddle and jQuery doesn't havecreateElement
method (as you are calling it as a jQuery method). You may use .append()Fiddle
您需要在 onclick 上传递对象
you need to pass the object on the onclick
因为在 OnClick 上您传递一个 String 作为第一个参数值,而在 js 调用中传递一个元素集合。
Because on OnClick you pass a String as first argument value and in js call a collection of elements is passed.