Javascript 如何使用;这";html中的关键字?
我试图在一个表中显示一个数据库,允许用户在单击时编辑每条记录。我有一个创建数据库对象的构造函数。我正在循环我的数据库来显示它。以下是代码的一部分:Javascript 如何使用;这";html中的关键字?,javascript,Javascript,我试图在一个表中显示一个数据库,允许用户在单击时编辑每条记录。我有一个创建数据库对象的构造函数。我正在循环我的数据库来显示它。以下是代码的一部分: for (var j = 1; j <this.data.length; j++) { var html = '<div id="this.data[j] + '"'; html += 'onclick="this.edit_record (\''+this.data[j]+'\')">'
for (var j = 1; j <this.data.length; j++) {
var html = '<div id="this.data[j] + '"';
html += 'onclick="this.edit_record (\''+this.data[j]+'\')">'+this.data[j]+'</div>';
}
$("#table").append(html);
for(var j=1;j您似乎缺少一个报价。更新了我的答案以解决此问题
var parent = this;
for (var j = 0; j < this.data.length; j++) {
var currentValue = this.data[j];
var html = '<div id="' + currentValue + '"';
html += '>' + currentValue + '</div>';
$('#table').append(html);
$('#'+currentValue).click(function(){
parent.edit_record ($(this).attr("id"));
});
}
var parent=this;
对于(var j=0;j
以下是小提琴链接您似乎缺少一个报价。更新了我的答案以解决此问题
var parent = this;
for (var j = 0; j < this.data.length; j++) {
var currentValue = this.data[j];
var html = '<div id="' + currentValue + '"';
html += '>' + currentValue + '</div>';
$('#table').append(html);
$('#'+currentValue).click(function(){
parent.edit_record ($(this).attr("id"));
});
}
var parent=this;
对于(var j=0;j
以下是onclick中的fiddle链接此
引用了单击的元素。在使用jquery时,最好是这样:
var html="", mythis=this; // save "this" (your object) context
for (var j = 1; j <this.data.length; j++) {
html += '<div data-id="'+this.data[j] + '">'+this.data[j]+'</div>';
}
$("#table").append(html); // append all html at once, faster!
// assign a click handler to all divs children of #table
$("#table>div").click(function() {
// inside this function, this is the clicked element (so this.getAttribute("data-id") would be the id
// and mythis would be the saved this (your object)
mythis.edit_record(this.getAttribute("data-id"));
});
var html=”“,mythis=this;//保存“this”(您的对象)上下文
对于(var j=1;j,onclick中的这个
引用了单击的元素。当您使用jquery时,最好是这样:
var html="", mythis=this; // save "this" (your object) context
for (var j = 1; j <this.data.length; j++) {
html += '<div data-id="'+this.data[j] + '">'+this.data[j]+'</div>';
}
$("#table").append(html); // append all html at once, faster!
// assign a click handler to all divs children of #table
$("#table>div").click(function() {
// inside this function, this is the clicked element (so this.getAttribute("data-id") would be the id
// and mythis would be the saved this (your object)
mythis.edit_record(this.getAttribute("data-id"));
});
var html=”“,mythis=this;//保存“this”(您的对象)上下文
对于(var j=1;j,没有必要在html本身中编写onclick
你可以这样处理:
请参阅代码注释
for (var j = 1; j <this.data.length; j++) {
// create html
var html = '<div id="' + this.data[j] + '"';
html += this.data[j] + '</div>';
// create new jQuery element
var e = $(html);
// set click handler.
var self = this; // closure for this
(function(x) { // iterator closure for j
e.click(function() {
self.edit_record(self.data[x])
});
})(j);
// append element.
$("#table").append(e);
}
for(var j=1;j无需在html本身中编写onclick
你可以这样处理:
请参阅代码注释
for (var j = 1; j <this.data.length; j++) {
// create html
var html = '<div id="' + this.data[j] + '"';
html += this.data[j] + '</div>';
// create new jQuery element
var e = $(html);
// set click handler.
var self = this; // closure for this
(function(x) { // iterator closure for j
e.click(function() {
self.edit_record(self.data[x])
});
})(j);
// append element.
$("#table").append(e);
}
用于(var j=1;j您可以使用HTML节点和事件监听器。您可以使用HTML节点和事件监听器。这仍然会失败,因为您正在将事件处理程序绑定到字符串,而不是元素,因此您正在对其进行解析、附加事件处理程序,然后丢弃元素。这仍然会失败,因为您正在绑定事件处理程序是一个字符串,而不是一个元素,因此您要解析它,附加一个事件处理程序,然后丢弃该元素。