问题:
我在使用 jQuery 时遇到了一点问题:考虑一下这个 HTML,我想在其中左键单击“https”,更改它的值,然后单击确定。确定后,它应该删除文本框,并重新附加 onclick 处理程序。确实如此,但当前单击已发出 onclick ...
为什么 ?以及如何正确地做到这一点?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Tabelle editieren</title>
<!--
<script src="jquery-1.4.4.min.js" type="text/javascript"></script>
-->
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.js"></script>
<style type="text/css" media="all">
html {margin: 0px; padding: 0px; height: 100%;}
body {margin: 0px; padding: 0px; height: 100%;}
input{margin: 0px; padding: 0px; }
tr:nth-child(2n+1) {background-color: #F0F0F0;}
tr:nth-child(2n) {background-color: #FEFEFE;}
td
{
margin: 0px; padding: 0.5cm; padding: 0px; text-align: left;
}
</style>
<script type="text/javascript" language="javascript">
function hello(me) {
//alert("hello");
var str = $(me).text();
//alert(str);
$(me).html("<input id=\"my1\" type=\"text\" style=\"width: 198px; height: 100%;\" value=\"" + str + "\" /><input id=\"my1confirm\" type=\"button\" onclick=\"bye(this);\"style=\"width: 35px;\" value=\"" + "OK" + "\" />");
$(me).attr('onclick', '').unbind('click');
return false;
}
function bye(me) {
var objTR = $(me).parent();
//var tb = $(me).closest('table');
var tb = objTR.find('input');
var str = tb.val();
tb.remove();
objTR.text(str);
alert("TODO: sending change event with { id : \"" + objTR.attr('id') + "\", str : \"" + str + "\" } to server");
/*
objTR.click(function () {
alert("hi");
return false;
});
*/
objTR.attr('onclick', '').bind("click", function (e) {
e.preventDefault();
alert("it shouldn't execute this in this click, but it does... ");
//hello(this);
//event.stopImmediatePropagation();
});
return false;
}
$(document).ready(function () {
//$('#myDataTable').dataTable().makeEditable();
});
</script>
</head>
<body style="margin: 0.5cm;">
<h1>Application configuration</h1>
<!--
-->
<table border="1" style="border-collapse: collapse;" >
<thead>
<tr style="background-color: Black; color:White; font-weight: bold; text-transform: capitalize;">
<th style="width: 235px; text-align: left;">key</th>
<th style="width: 235px; text-align: left;">value</th>
</tr>
</thead>
<tfoot>
<tr style="background-color: Black; color:White; font-weight: bold; text-transform: capitalize;">
<th style="text-align: left;">key</th>
<th style="text-align: left;">value</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Environment</td>
<td id="server">testing</td>
</tr>
<tr>
<td>Protocol</td>
<td id="Protocol" onclick="hello(this);">https</td>
</tr>
<tr>
<td>UserHashMode</td>
<td id="UserHashMode">true</td>
</tr>
<tr>
<td>Logo</td>
<td id="Logo">LogoFileCustomerN.png</td>
</tr>
</tbody>
</table>
</body>
</html>