如果您正在使用jQuery,那么:
HTML:
<a id="openMap" href="/map/">link</a>
聯署材料:
$(document).ready(function() {
$("#openMap").click(function(){
popup('/map/', 300, 300, 'map');
return false;
});});
這樣做的好處是沒有JS仍然工作,或者如果用戶中間單擊鏈接。
這也意味著我可以通過再次將以下內容重寫來處理通用彈出窗口:
HTML:
<a class="popup" href="/map/">link</a>
聯署材料:
$(document).ready(function() {
$(".popup").click(function(){
popup($(this).attr("href"), 300, 300, 'map');
return false;
});});
這將允許您添加一個彈出窗口到任何鏈接,只要給它彈出類。
這一想法還可以進一步推廣,如下所示:
HTML:
<a class="popup" data-width="300" data-height="300" href="/map/">link</a>
聯署材料:
$(document).ready(function() {
$(".popup").click(function(){
popup($(this).attr("href"), $(this).data('width'), $(this).data('height'), 'map');
return false;
});});
現在,我可以使用相同的代碼在我的整個網站上的許多彈出窗口,而不必編寫大量的點擊材料!可重用性太好了!
這也意味著,如果稍后我決定彈出窗口是錯誤的做法,(他們是!)如果我想用一個Lightbox樣式的模態窗口來代替它們,我可以更改:
popup($(this).attr("href"), $(this).data('width'), $(this).data('height'), 'map');
到
myAmazingModalWindow($(this).attr("href"), $(this).data('width'), $(this).data('height'), 'map');
我在整個網站上的彈出窗口現在都完全不同了。我甚至可以做特征檢測來決定在彈出窗口上做什么,或者存儲用戶偏好以允許或不允許他們。隨著內聯點擊,這需要一個巨大的復制和粘貼努力。