我在界面上有一个DIV来装数据,先影藏起来,当一个文本框获(Text)取焦点的时候在DIV里面动态添加一个Table,每个TR有一个onclick="RowClick(this)" 事件,onclick事件是用来获取点击行的数据然后赋给文本框(Text),文本框还有个事件是onblur,当文本框获取值之后隐藏DIV,这时先执行的是文本框的onblur事件,而没有执行TR上面的onclick事件。我需要点击table时执行的是TR上的onclick事件,各位大神,怎么实现呢?
<script type="text/javascript"> // 创建一个去掉前后空格的公共方法
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, "");
}; $(function () { $("#txtFromCity").keyup(function () {
document.getElementById("selectFromCity").innerHTML = "";
$("#selectFromCity").attr("style", "display:block;");
var CityAbbreviated = $("#hideCityAbbreviated").val().trim();
var txtFromCity = $("#txtFromCity").val().trim();
var reg = /[^a-zA-Z\u4e00-\u9fa5]+/gi;
var reg1 = /[a-zA-Z]+/gi;
if (reg.test(txtFromCity) == true) {
document.getElementById("lblFromCity").innerHTML = "请输入正确的城市名称!";
return;
}
else {
$("#selectFromCity").append('<table id="tbFromCity" style="background-color:#60C3EC">');
if (reg1.test(txtFromCity) == true) {
var str = CityAbbreviated.split(',');
for (var i = 0; i < str.length; i++) {
if (txtFromCity.length == "1") {
if (txtFromCity.toUpperCase() == str[i].substring(0, 1) || txtFromCity.toUpperCase() == str[i].substring(str[i].length - 3, str[i].length - 2)) {
$("#selectFromCity").append('<tr onclick="RowClick(this)" class="tr"><td>' + str[i] + '</td></tr>');
}
}
}
});
}); // TR的行点击事件
var currentRow = null;
function RowClick(row) {
if (currentRow != null) {
currentRow.style.removeAttribute("backgroundColor");
}
currentRow = row;
currentRow.style.backgroundColor = "#60C3EC";
var fromCity = currentRow.cells[0].innerHTML;
var str = fromCity.trim().split('-');
$("#txtFromCity").val(str[1]);
$("#selectFromCity").attr("style", "display:none;");
}
function HideDiv() {
$("#selectFromCity").attr("style", "display:none;");
}
</script> <table>
<tr>
<td>
出发城市:
</td>
<td>
<input id="txtFromCity" onblur="HideDiv()" /><label id="lblFromCity"></label>
<div id="selectFromCity">
</div>
</td>
</table>
<script type="text/javascript"> // 创建一个去掉前后空格的公共方法
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, "");
}; $(function () { $("#txtFromCity").keyup(function () {
document.getElementById("selectFromCity").innerHTML = "";
$("#selectFromCity").attr("style", "display:block;");
var CityAbbreviated = $("#hideCityAbbreviated").val().trim();
var txtFromCity = $("#txtFromCity").val().trim();
var reg = /[^a-zA-Z\u4e00-\u9fa5]+/gi;
var reg1 = /[a-zA-Z]+/gi;
if (reg.test(txtFromCity) == true) {
document.getElementById("lblFromCity").innerHTML = "请输入正确的城市名称!";
return;
}
else {
$("#selectFromCity").append('<table id="tbFromCity" style="background-color:#60C3EC">');
if (reg1.test(txtFromCity) == true) {
var str = CityAbbreviated.split(',');
for (var i = 0; i < str.length; i++) {
if (txtFromCity.length == "1") {
if (txtFromCity.toUpperCase() == str[i].substring(0, 1) || txtFromCity.toUpperCase() == str[i].substring(str[i].length - 3, str[i].length - 2)) {
$("#selectFromCity").append('<tr onclick="RowClick(this)" class="tr"><td>' + str[i] + '</td></tr>');
}
}
}
});
}); // TR的行点击事件
var currentRow = null;
function RowClick(row) {
if (currentRow != null) {
currentRow.style.removeAttribute("backgroundColor");
}
currentRow = row;
currentRow.style.backgroundColor = "#60C3EC";
var fromCity = currentRow.cells[0].innerHTML;
var str = fromCity.trim().split('-');
$("#txtFromCity").val(str[1]);
$("#selectFromCity").attr("style", "display:none;");
}
function HideDiv() {
$("#selectFromCity").attr("style", "display:none;");
}
</script> <table>
<tr>
<td>
出发城市:
</td>
<td>
<input id="txtFromCity" onblur="HideDiv()" /><label id="lblFromCity"></label>
<div id="selectFromCity">
</div>
</td>
</table>
(目的是为触发失去焦点事件)
现在点击这个table有两个事件,一个是onclick事件,一个Onfocus事件,在点击的时候怎么判断a是true还是false呢?
你是在onblur里面判断,这时已经进入onblur事件了,现在是点击TR时触发的是两个函数,onclick和onblur,默认执行的是onblur,现在要让他执行onclick事件
<head>
<script>
function a()
{
alert("失去焦点事件");
}
function b()
{
alert("点击行事件");
}</script>
</head>
<body>
<table>
<tr onclick="b()">
<td height="40px" width="60px" style="background:black">
<input type="text" style="width:30px;height:20px" onblur="a()"/>
</td>
</tr>
</table></body>
</html>
<head>
<script>
var kg = false;
function a()
{
if(kg)
alert("点击行后执行的失去焦点事件");
}
function b()
{
alert("点击行事件");
kg= true;
document.getElementById("tex").focus();
document.getElementById("tex").blur();
}</script>
</head>
<body>
<table>
<tr onclick="b()">
<td height="40px" width="60px" style="background:black">
<input id="tex" type="text" style="width:30px;height:20px" onblur="a()"/>
</td>
</tr>
</table></body>
</html>
{
if(kg)
{
alert("点击行后执行的失去焦点事件");
kg=false;//执行后重置开关状态
} }
<table>
<tr onclick="b(this)">
<td height="40px" width="60px" style="background:black">
<input id="tex" type="text" style="width:30px;height:20px" onblur="a()"/>
</td>
</tr>
</table>执行b事件的时候还要获取此行数据,你是想失去焦点时当判断kg==false时执行a事件,这是不行的,这样就不能获取数据了