有一个项目,AJAX更改订单价格,当点击价格的时候,价格变成可输入的input我想让它在点击body的任何一个位置,都可以触发改变价格的函数但是点击input这个输入框是不会触发任何事件所以我用$(body:not())但是怎么测试都不行,点任何位置都会触发alert,包括id=a_c
<td> <span onclick="change_price(this,43)">89.00</span> </td>change_price=function(get_td,rec_id){
$price_td=$(get_td);
$price=$price_td.html();
span="<div id='a_c'><input style='width:50px' value='"+$price+"'></div>";
$price_td.after(span);
$price_td.html("");
$("body:not(#a_c)").click(function(){
alert("body")
})}
<td> <span onclick="change_price(this,43)">89.00</span> </td>change_price=function(get_td,rec_id){
$price_td=$(get_td);
$price=$price_td.html();
span="<div id='a_c'><input style='width:50px' value='"+$price+"'></div>";
$price_td.after(span);
$price_td.html("");
$("body:not(#a_c)").click(function(){
alert("body")
})}
参考下面的实现:
$('body').click(function(e) {
if ($(e.target).parents().andSelf().index(span) == -1) {
alert("body");
}
});
测试过了用onblur和onchange
但是实现的起来非常的不理想,也很不及时!
$('body').click(function(e) {
if ($(e.target).parents().andSelf().index(span) == -1) {
alert("body");
}
});就是实现你想要的那个功能,e.target是你点击的区域,判断这个区域是不是在特定的span里面,因为
jQuery里面没有Prototype的descendantsOf,所以用$(e.target).parents().andSelf().index(span)这种方式来判断。
测试了一下,还是不行,点击那个编辑框input还是会执行alert