聆听DIV中的更改并采取相应措施 [英] Listen to changes within a DIV and act accordingly
问题描述
我有一个获取XML文档并根据XSL文档对其进行转换的函数.然后将结果放入ID为laneconfigdisplay
的div中.我想做的是,独立于转换逻辑,为该div设置一个jQuery change事件,以便我可以知道它何时已更改,并运行一些jQuery代码.
I have a function that grabs an XML document and transforms it according to an XSL document. It then places the result into a div with the id laneconfigdisplay
. What I want to do is, separate to the transformation logic, setup a jQuery change event for that div so I can tell when it has changed, and run some jQuery code.
我尝试了以下方法,但是不起作用!
I have tried the following, but it does not work!
$(document).ready(function()
{
$('#laneconfigdisplay').change(function() {
alert('woo');
});
//Do XML / XSL transformation here
});
<!-- HTML code here -->
<div id="laneconfigdisplay"></div>
我在做什么错了?
推荐答案
您可以选择创建自己的自定义事件,以使逻辑仍然清晰分开.
You can opt to create your own custom events so you'll still have a clear separation of logic.
绑定到自定义事件:
$('#laneconfigdisplay').bind('contentchanged', function() {
// do something after the div content has changed
alert('woo');
});
在更新div的函数中:
In your function that updates the div:
// all logic for grabbing xml and updating the div here ..
// and then send a message/event that we have updated the div
$('#laneconfigdisplay').trigger('contentchanged'); // this will call the function above
这篇关于聆听DIV中的更改并采取相应措施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!