Javascript 如何对同一对象的相同单击事件排序
我的html页面中有一个对象列表 基于注释编辑:这是我的XMLJavascript 如何对同一对象的相同单击事件排序,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,我的html页面中有一个对象列表 基于注释编辑:这是我的XML <?xml version="1.0" encoding="utf-8"?> <Root> <Item> <Property_Cat_ID>1</Property_Cat_ID> <Property_Cat_Name>Buy</Property_Cat_Name> </Item> <Item>
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Item>
<Property_Cat_ID>1</Property_Cat_ID>
<Property_Cat_Name>Buy</Property_Cat_Name>
</Item>
<Item>
<Property_Cat_ID>2</Property_Cat_ID>
<Property_Cat_Name>Rent</Property_Cat_Name>
</Item>
</Root>
我的问题是哪个函数将首先执行,以及如何调用我的getValues(0)代码>仅在执行my effect.js click函数后作为单击事件
我希望我能够解释,我的问题,在这里写和解释太大太复杂了。你无法精确控制绑定事件的执行顺序(除非你引入一些不太干净的计时机制)
但是,在您的情况下,为了确保effect.js操作在其他操作之前执行,您可以将effect.js中的绑定从“click”更改为“mousedown”(确保不调用preventDefault或从事件处理程序返回false)
编辑
在effect.js中绑定mouseup
(而不是mousedown
)-查看此提琴的演示:如果需要实现此行为,则必须使用自定义事件。将ajax数据加载事件包装在自定义事件中,然后从单击事件触发该事件,反之亦然
作为参考,这方面有一个完整的实现。您无法控制事件处理程序的执行顺序,但通过将两个单击事件处理程序合并,您可以控制希望在同一处理程序中执行的事情的顺序
您的示例代码包含两个错误,但我想我已经修复了大部分错误
$("div.ddListData ul").on('click', 'li', function() {
console.log('Triggered click event, this =', this); // DEBUG
// Call getValue()
getValues(0); // function which does dynamic value assigning work to all my fields
// NOTE: Declaring elements to reduce lookups
var $this = $(this);
var $parent = $this.parent('ul').parent('div').parent('div');
var $ddBoxes = $parent.prev('.ddBox');
// NOTE: You should be able to find a shorter way to select the element(s).
// Hide .ddBoxes and set value of its first input
$ddBoxes.hide().find('input:first').val($this.html());
// Set value of the last input
$ddBoxes.find('input:last').val(this.id);
// Hide some parent element
$parent.hide();
});
及
var$xml=$(数据);
var$items=$xml.find('Item');
$items.每个(功能(索引){
var$this=$(this);
var id=$this.find('Property_Cat_id').text();
var name=$this.find('Property_Cat_name').text();
如果(索引==0){//第一项
}
$(“”)html(名称)
.附录(
$('.ddpan02.ddListData ul')
//$(“div.tabdpan02”).find(“div.ddpan div.ddpan01 div.ddBoxHold div.ddList div.ddListData ul”)//无效,并指向长选择器
);
});
查看上的完整解决方案,我已经尝试过了,但它对我无效!任何其他值得赞赏的解决方案编辑:将我提到的mosuedown更改为mouseup。请参阅更新的答案。顺便说一句,感谢您分享您的知识-我已经尝试了$(“.ddListData ul li”).live('mouseup',function()
我的代码不起作用,即使它没有给出任何错误,也会把我的头撞到我的桌子上。你能为ajax响应提供一些示例XML数据吗?谢谢,玩了它并发布了一个答案,希望它能帮到我。我正在编写你的代码并实现这些东西,但因为我只放了代码的一部分,我在执行时面临着很大的问题曼汀,希望我能找到一个更好的方法来处理你的指令……谢谢你的帮助。
$("div.ddpan01 div.ddBoxHold").find("div.ddList").find("div.ddListData ul li").click(function () {
alert('click');
getValues(0); // function which does dynamic value assigning work to all my fields
})
$.ajax({
type: "GET",
url: "/Admin/Includes/XML/PropertyCategory.xml",
dataType: "xml",
success: function (xml) {
var count = 1;
$(xml).find('Item').each(function () {
var id = $(this).find('Property_Cat_ID').text();
var name = $(this).find('Property_Cat_Name').text();
if (count == 1) {
$("div.tabdpan02").find("div.ddpan div.ddpan01 div.ddBoxHold div.ddBox input:first").val(name);
$("div.tabdpan02").find("div.ddpan div.ddpan01 div.ddBoxHold div.ddBox input:last").val(id);
}
$("<li id=" + id + "></li>").html(name).appendTo($("div.tabdpan02").find("div.ddpan div.ddpan01 div.ddBoxHold div.ddList div.ddListData ul"));
count++
});
}
});
$(".ddListData ul li").live('click', function () {
var htmlStr = $(this).html();
var hId = $(this).attr('id');
$(this).parent('ul').parent('div').parent('div').prev('.ddBox').find('input:first').val(htmlStr);
$(this).parent('ul').parent('div').parent('div').prev('.ddBox').find('input:last').val(hId);
$(this).parent('ul').parent('div').parent('div').hide(0);
})
$("div.ddListData ul").on('click', 'li', function() {
console.log('Triggered click event, this =', this); // DEBUG
// Call getValue()
getValues(0); // function which does dynamic value assigning work to all my fields
// NOTE: Declaring elements to reduce lookups
var $this = $(this);
var $parent = $this.parent('ul').parent('div').parent('div');
var $ddBoxes = $parent.prev('.ddBox');
// NOTE: You should be able to find a shorter way to select the element(s).
// Hide .ddBoxes and set value of its first input
$ddBoxes.hide().find('input:first').val($this.html());
// Set value of the last input
$ddBoxes.find('input:last').val(this.id);
// Hide some parent element
$parent.hide();
});
var $xml = $(data);
var $items = $xml.find('Item');
$items.each(function (index) {
var $this = $(this);
var id = $this.find('Property_Cat_ID').text();
var name = $this.find('Property_Cat_Name').text();
if (index == 0) { // First item
}
$("<li id=" + id + "></li>").html(name)
.appendTo(
$('.ddpan02 .ddListData ul')
//$("div.tabdpan02").find("div.ddpan div.ddpan01 div.ddBoxHold div.ddList div.ddListData ul") // Invalid and way to long selector
);
});