2013-11-04 20 views
0

我想將我的代碼簡化爲一個完整的步驟函數。由於它具有相同的屬性&函數,我不知道在一個動作中完全合併它的調用函數。裸露在一起,我只是一個noob jQuery用戶。提前感謝您的建議。簡化jQuery DOM請求

$('a[data-user="add"]').click(function(event) { 
    event.preventDefault(); 
    $('.list-unstyled li').removeClass('active'); 
    $('section.col-lg-8').replaceWith(formAdd); 
    $('.form-custom').load('includes/pages/admin/admin-user-add.php'); 
    subHash = this.hash; 
}); 

$('a[data-user="settings"]').click(function(event) { 
    event.preventDefault(); 
    $('.list-unstyled li').removeClass('active'); 
    $('section.col-lg-8').replaceWith(formAdd); 
    $('.form-custom').load('includes/pages/admin/admin-user-settings.php'); 
    subHash = this.hash; 
}); 

我認爲包括它與兩個單獨的逗號,將是一個好主意,但它不是。它默認視圖爲我的第一次加載。

+0

哪裏是你的HTML –

+0

你的意思是這個劇本的整個代碼?或整個頁面? – iMarkDesigns

回答

1

嘗試抓住用逗號分隔的兩個選擇,然後使用jQuery的.attr()功能,填補了正確的管理頁面:

$('a[data-user="settings"],a[data-user="add"]').click(function(event) { 
    event.preventDefault(); 
    $('.list-unstyled li').removeClass('active'); 
    $('section.col-lg-8').replaceWith(formAdd); 
    $('.form-custom').load('includes/pages/admin/admin-user-'+$(this).attr('data-user')+'.php'); 
    subHash = this.hash; 
}); 
+0

非常感謝這個想法。它現在工作正常。 :) – iMarkDesigns