2015-02-10 24 views
0

請問我可以告訴我如何給查詢按鈕分配一個id。我使用的查詢對話框模式窗體(http://jqueryui.com/dialog/#modal-form) - 試圖樣式的按鈕,但它不工作JQUERY分配id給jquery對話框按鈕

我的查詢代碼:

buttons: { 
     "Create an account": addUser, 
     Cancel: function() { 
     dialog.dialog("close"); 
     } 
    }, 

我嘗試添加它作爲以下,但它沒有工作:

buttons: { 
     "Create an account": { addUser, 
     id: "account-btn" 
     Cancel: function() { 
      dialog.dialog("close"); 
     } 
     } 
    }, 
+0

如果你想樣式的按鈕來添加身份證在你的HTML。在你提供的那個鏈接上,如果你點擊查看源代碼,然後滾動到最下面你會看到按鈕id在html – Craicerjack 2015-02-10 23:22:36

+0

中設置,「創建帳戶」按鈕不會顯示 - 這是一個隱藏的按鈕,我不確定如何訪問和風格。你指的是「創建新用戶」按鈕,但我在尋找「創建一個帳戶按鈕」,以添加一個ID來設計它的樣式 – ARTLoe 2015-02-10 23:28:51

+0

對不起,沒有看到。實際上看你的代碼你做了一些奇怪的按鈕 - 回答下面添加 – Craicerjack 2015-02-11 10:47:57

回答

0

他們有一類ui-button。所以,你可以只瞄準他們在CSS像這樣:

.dialogForm > .ui-button { 
    /* styles here */ 
} 

或針對特定的按鈕

.dialogForm > .ui-button:nth-of-type(1) { 
    /* targets the first button */ 
} 

同樣可以用JS針對他們並添加類,但它的混亂。

  • 編輯:如果您試圖添加樣式的id,請考慮使用類代替。不過,您可以在第一個按鈕上添加一個ID。這是jQuery模式爲按鈕視圖創建的標記。

$(function() { 
 
    // If you want to target another button use 'button.ui-button:nth-child(INT)'. 
 
    // Remove the selector and colon if you wish to target both. 
 
    var $button = $('div.ui-dialog-buttonpane button.ui-button:first'); 
 

 
    // Adds id attribute. The styling here is just to prove we're getting the right thing. 
 
    $button.attr('id', 'uglyId'); 
 
    $button.css('background-color', 'pink'); 
 

 
    console.log($button.attr('id')); 
 
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <script src="//code.jquery.com/jquery-2.1.1.min.js"></script> 
 
    <meta charset="utf-8"> 
 
    <title>JS Bin</title> 
 
</head> 
 

 
<body> 
 
    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"> 
 
    <div class="ui-dialog-buttonset"> 
 

 
     <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button"> 
 
     <span class="ui-button-text">Create an account</span> 
 
     </button> 
 
     <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button"> 
 
     <span class="ui-button-text">Cancel</span> 
 
     </button> 
 

 
    </div> 
 
    </div> 
 

 
</body> 
 

 
</html>

+0

我正在尋找一個特定的按鈕,並想知道如何分配一個id到該特定的按鈕,因爲它的造型是不同於其他按鈕 – ARTLoe 2015-02-11 00:01:09

+0

回答。僅供參考,請使用DevTools或Firebug查看HTML標記。然後,您可以決定哪些元素具有可以綁定的道具。 – 2015-02-11 00:46:49

0

你正在創建的按鈕中的一個對象,並試圖將ID添加到,而不是添加id來按鈕。嘗試這個。

buttons: { 
    id: "account-btn", //id as a seperate attribute 
    "Create an account": addUser, 
    Cancel: function() { 
      dialog.dialog("close"); 
    } 
} 

或給予相應的ID只爲造型,你可以這樣做:
給你的對話形式的ID,然後在你的CSS

#dialog_form_id button { 
    //add your style here 
}