2014-04-07 21 views
0

我有下面的代碼:我應該繼續使用的document.ready Jquery的

$(document).ready(function(){ 
    $('td.rightImage, .leftImage a').click(function(){ 
    if($(this).attr("id")){ 
    _gaq.push(['_trackEvent', 'sidebanner', 'click', $(this).attr('id'),0,true]); 
    } 
else{ 
_gaq.push(['_trackEvent', 'sidebanner', 'click', $(this).attr('href'),0,true]); 
} 
}); 
}); 

    $(document).ready(function(){ 
    $("a[href$='.pdf']").click(function(){ 
    _gaq.push(['_trackEvent', 'pdf', 'click',$(this).attr('href'),0,true]); 
    }); 
    }); 
    $(document).ready(function(){ 
    $("a[href$='.zip']").click (function(){ 
    _gaq.push(['_trackEvent', 'zip', 'download',$(this).attr('href'),0,true]); 
    }); 
    }); 

我應該繼續使用的Docment準備每個onclick事件我想如果不是有什麼更好的方式來編寫上面?

感謝

+2

讓它只是一個功能,它們都在同一時間被調用,畢竟。 – George

+0

是!如果使用jQuery或其他框架或純JS,則無關緊要。只要記住當所有元素已經在jQuery中表示'document.ready()'的位置時執行這種代碼。你的腳本在它可用時立即執行 - 甚至在加載HTML文檔之前 - 這就是'document.ready'或'body.onload'代表的! – ElmoVanKielmo

回答

1

使用您可以將它們合併和$(文件)。就緒可以這樣寫。

$(function(){ 
    // DOM Ready - do your stuff 
}); 
2

有一個更好的辦法 - 把他們都在一個DOM準備的事件。上的document.ready

//OR 
//$(function(){ 
$(document).ready(function() { 
    $('td.rightImage, .leftImage a').click(function() { 
     if ($(this).attr("id")) { 
      _gaq.push(['_trackEvent', 'sidebanner', 'click', $(this).attr('id'), 0, true]); 
     } else { 
      _gaq.push(['_trackEvent', 'sidebanner', 'click', $(this).attr('href'), 0, true]); 
     } 
    }); 
    $("a[href$='.pdf']").click(function() { 
     _gaq.push(['_trackEvent', 'pdf', 'click', $(this).attr('href'), 0, true]); 
    }); 
    $("a[href$='.zip']").click(function() { 
     _gaq.push(['_trackEvent', 'zip', 'download', $(this).attr('href'), 0, true]); 
    }); 
}); 
1

相關問題