Javascript jQuery中的错误事件绑定
我对数据表上的数据行进行了排序和搜索。此外,当单击数据行时,它会提醒其数据行的名称和代码。有人对我的代码发表了评论:“事件绑定很糟糕,很弱!”。我是在一次采访中得到这个评论的,所以我没有机会问为什么。我希望有人能找出问题所在Javascript jQuery中的错误事件绑定,javascript,jquery,datatables,jquery-events,Javascript,Jquery,Datatables,Jquery Events,我对数据表上的数据行进行了排序和搜索。此外,当单击数据行时,它会提醒其数据行的名称和代码。有人对我的代码发表了评论:“事件绑定很糟糕,很弱!”。我是在一次采访中得到这个评论的,所以我没有机会问为什么。我希望有人能找出问题所在 <!--HTML--> <table class="" id="tabCountryList"> <thead> <tr> <th
<!--HTML-->
<table class="" id="tabCountryList">
<thead>
<tr>
<th class="" tabindex="">Name</th>
<th class="" tabindex="">Code</th>
</tr>
</thead>
<tbody id="tbCountryListContent">
</tbody>
</table>
<!--javascript-->
var tbContent="";
for(var idx in countries){
tbContent+="<tr class='countryRow'><td>"+countries[idx].name+"</td>
<td>"+countries[idx].code+"</td></tr>";
}
document.getElementById("tbCountryListContent").innerHTML=tbContent;
$(document).ready(function(){
$("#tabCountryList").DataTable({"responsive": true, "dom":
'<"toolbar">frtip',"displayLength": 4 ,"aaSorting": []});
$(".countryRow").click(function() {
var tempObj = $(this).find("td");
alert(tempObj[0].innerHTML);
});
$(".toolbar").hide();
});
名称
代码
var-tbContent=“”;
用于(国家/地区的风险值idx){
tbContent+=“”+国家[idx]。名称+“
“+国家[idx]。代码+”;
}
document.getElementById(“tbCountryListContent”).innerHTML=tbContent;
$(文档).ready(函数(){
$(“#tabCountryList”).DataTable({“responsive”:true,“dom”:
“frtip',“displayLength”:4,“aaSorting”:[]});
$(“.countryRow”)。单击(函数(){
var tempObj=$(this.find(“td”);
警报(tempObj[0].innerHTML);
});
$(“.toolbar”).hide();
});
以下是生成的图像:
您可以在方法上使用
jquery
,而不是直接使用。这可能是一个问题。例如,$(“.class”)。在(“click”,function(){})上代码>而不是$(“.class”)。单击(函数(){})代码>谢谢你的提示。