簡體   English   中英

HTML 和 JQuery 中的切換按鈕

[英]Toggle button in HTML and JQuery

我正在嘗試使用以下方法創建一個切換按鈕,但它不起作用。 我已經包含了我正在使用的 CSS、HTML 和 javascript 片段。 如果我做的不對,請提出建議。

CSS:

.toggle-button {
  margin-bottom: 15px;
}
.toggle-button img {
  display:    inline-block;
  height:     30px;
  width:      60px;
  background: url(../images/on-button.png) no-repeat center center;
}
.toggle-button img.down {
  background: url(../images/off-button.png) no-repeat center center;
} 

HTML:

 <head>
 <script Language="JavaScript">
     function toggle (img) {  
         $(img).toggleClass("down");
     }
 </script>
 </head>
 <div class="right-form">
     <h2>Email Settings</h2>
     <div class="toggle-button">
         <label>Stock Require Scanning:</label>
         <img ondblclick="toggle(this);" border="0 id="stock_require_scanning"/>
     </div>                    
     <div value="OFF" class="toggle-button">
         <label>Head Office Alerts:</label>
         <img ondblclick="toggle(this);" class="down" border="0" id="head_office_alerts"/>
     </div>                                    
 </div>

有什么我做錯的嗎?

這里有一個正在運行的演示: http : //jsfiddle.net/L9nHY/ (我使用綠色表示“開”,紅色表示“下來”)。 你在這里犯了一個錯誤:

<img ondblclick="toggle(this);" border="0"    id="stock_require_scanning"/>

你必須用'#stock_require_scanning'替換this

<img ondblclick="toggle('#stock_require_scanning');" border="0"    id="stock_require_scanning"/>

您必須對另一個按鈕執行相同操作。

編輯:Sree:另見: http : //jsfiddle.net/L9nHY/1/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM