JavaScript 纯JS手动触发事件
2012-10-19 11:37:35 917

引用:http://www.guahanweb.com/2010/03/02/how-to-manually-trigger-events-in-javascript/

// Here is a VERY basic generic trigger method
function triggerEvent(el, type)
{
    if ((el[type] || false) && typeof el[type] == 'function')
    {
        el[type](el);
    }
}
 
// We could call this on multiple objects at any time
function resetFields()
{
    triggerEvent(document.getElementById('has-email'), 'onchange');
    triggerEvent(document.getElementById('other-field'), 'onclick');
    triggerEvent(document.getEleemntById('another-one'), 'onblur');
}

//单独的
var el = document.getElementById('has-email');
if ((el.onchange || false) && typeof el.onchange == 'function')
{
    // We have a valid onchange listener declared
    el.onchange({target : el});
}