TML提交按鈕是一種HTML表單元素,允許用戶將表單數(shù)據(jù)提交到服務(wù)器。提交按鈕通常與表單元素(如文本框和下拉列表)一起使用,以便用戶可以輸入并提交信息。在HTML中,提交按鈕通常使用標(biāo)簽來定義。
如何編寫HTML提交按鈕代碼?
要創(chuàng)建HTML提交按鈕,您需要使用標(biāo)簽,并將type屬性設(shè)置為“submit”。例如,以下代碼會創(chuàng)建一個名為“submit”的提交按鈕:
```
```
在這個例子中,“action”屬性指定了表單數(shù)據(jù)提交到的URL,“method”屬性指定了提交表單的HTTP方法(通常是POST或GET)。按鈕的“value”屬性指定了按鈕上顯示的文本。
如何自定義HTML提交按鈕樣式?
默認(rèn)情況下,HTML提交按鈕的樣式取決于用戶的操作系統(tǒng)和瀏覽器。但是,您可以使用CSS樣式表來自定義按鈕的外觀。例如,以下代碼將創(chuàng)建一個紅色的提交按鈕:
```
```
在這個例子中,我們使用了style屬性來設(shè)置按鈕的背景顏色和文本顏色。您還可以使用其他CSS屬性來自定義按鈕的大小、邊框等。
如何使用JavaScript處理HTML提交按鈕?
您可以使用JavaScript來添加交互性和驗證表單數(shù)據(jù)。例如,以下代碼將在用戶單擊提交按鈕時彈出一個提示框:
```
```
在這個例子中,我們使用了onsubmit屬性來指定當(dāng)表單提交時要運行的JavaScript函數(shù)。此函數(shù)返回true或false,如果返回false,則表單將不會提交。在這個例子中,我們使用confirm()函數(shù)顯示一個提示框,并在用戶單擊“確定”時返回true。
總結(jié)
HTML提交按鈕是Web表單中的重要元素,允許用戶將表單數(shù)據(jù)提交到服務(wù)器。您可以使用標(biāo)簽來創(chuàng)建提交按鈕,并使用CSS樣式表自定義外觀。您還可以使用JavaScript添加交互性和驗證表單數(shù)據(jù)。通過掌握HTML提交按鈕的知識,您可以創(chuàng)建復(fù)雜的Web表單,并收集和處理用戶數(shù)據(jù)。
lt;link rel="stylesheet" type="text/css">
<style>
.red-text {
color: red;
}
h2 {
font-family: Lobster, Monospace;
}
p {
font-size: 16px;
font-family: Monospace;
}
.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius: 50%;
}
.smaller-image {
width: 100px;
}
</style>
<h2 class="red-text">CatPhotoApp</h2>
<p>Click here for <a href="#">cat photos</a>.</p>
<a href="#"><img class="smaller-image thick-green-border" alt="A cute orange cat lying on its back" src="/statics/codecamp/images/relaxing-cat.jpg"></a>
<p>Things cats love:</p>
<ul>
<li>cat nip</li>
<li>laser pointers</li>
<li>lasagna</li>
</ul>
<p>Top 3 things cats hate:</p>
<ol>
<li>flea treatment</li>
<li>thunder</li>
<li>other cats</li>
</ol>
<form action="/submit-cat-photo">
<label><input type="radio" name="indoor-outdoor"> Indoor</label>
<label><input type="radio" name="indoor-outdoor"> Outdoor</label>
<input type="text" placeholder="cat photo URL" required>
<button type="submit">Submit</button>
</form>
一篇文章Stimulus:連接HTML和JavaScript的橋梁,實現(xiàn)簡單的controller,并學(xué)習(xí)了Stimulus是如何連接HTML與JavaScript的。現(xiàn)在我們使用Stimulus來實現(xiàn)復(fù)制文本到粘貼板的按鈕。
比如說,我們現(xiàn)在有一個需求,就是幫助用戶生成密碼,在密碼旁邊放置一個按鈕,點擊按鈕后密碼就被拷貝到粘貼板上了,這樣就方便用戶使用這個密碼了。
打開public/index.html,修改body內(nèi)容,填充一個簡單的按鈕,如下:
<div>
PIN: <input type="text" value="1234" readonly>
<button>Copy to Clipboard</button>
</div>
下一步,創(chuàng)建src/controllers/clipboard_controller.js,然后添加一個copy()方法:
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
copy() {
}
}
然后,給div添加data-controller=“clipboard”。只要是給元素添加了data-controller屬性,Stimulus就會連接一個controller實例。
<div data-controller="clipboard">
我們還需要一個對輸入框的引用,這樣我們就可以在調(diào)用粘貼板API之前獲取輸入框的內(nèi)容。給文本框添加data-clipboard-target=“source“:
PIN: <input data-clipboard-target="source" type="text" value="1234" readonly>
在controller中定義一個target,然后就可以通過this.sourceTarget訪問文本框了。
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "source" ]
copy() {
}
}
解釋一下這個targets:
當(dāng)Stimulus加載你的controller類時,它會查看靜態(tài)數(shù)組targets的字符串元素,對于每一個字符串,Stimulus會在controller中添加3個屬性。在這里,對于“source”,會添加如下屬性:
this.sourceTarget 在controller的域內(nèi)的第一個source
this.sourceTargets 在controller的域內(nèi)所有的source組成的一個數(shù)組
this.hasSourceTarget 在controller的域內(nèi)是否有source
我們希望點擊按鈕時調(diào)用controller中的copy()方法,所以我們需要添加data-action=“clipboard#copy“
<button data-action="clipboard#copy">Copy to Clipboard</button>
你可以已經(jīng)注意到在上面的動作描述符中省略了click->。那是因為Stimulus給button設(shè)置了click作為它默認(rèn)的事件。
某些其他元素也有默認(rèn)事件。下面是個全部列表:
元素 | 默認(rèn)事件 |
a | click |
button | click |
details | toggle |
form | submit |
input | input |
input type=“submit” | click |
select | change |
textarea | input |
最終,在copy()方法中,我們獲取輸入框的內(nèi)容,調(diào)用粘貼板API
copy() {
navigator.clipboard.writeText(this.sourceTarget.value)
}
刷新頁面,點擊按鈕,然后快捷鍵粘貼到Greet按鈕前到輸入框,可以看到1234。
到目前為止,在頁面上同一時間只有一個controller實例。在頁面上同時有一個controller的多個實例也是很正常的。
我們的controller是可以復(fù)用的,只要你需要在頁面上添加復(fù)制內(nèi)容的按鈕,無論是哪個頁面,只要把對應(yīng)的屬性值寫好,我們的controller都是生效的。
還是上面的例子,再添加另外一個復(fù)制按鈕:
<div data-controller="clipboard">
PIN: <input data-clipboard-target="source" type="text" value="3737" readonly>
<button data-action="clipboard#copy" class="clipboard-button">Copy to Clipboard</button>
</div>
刷新頁面,驗證一下兩個復(fù)制按鈕是否都生效。
我們再添加一個可以復(fù)制的元素,不用button,我們用a標(biāo)簽,
<div data-controller="clipboard">
PIN: <input data-clipboard-target="source" type="text" value="6666" readonly>
<a href="#" data-action="clipboard#copy" class="clipboard-button">Copy to Clipboard</a>
</div>
Stimulus允許我們使用任何元素,只要它設(shè)置了合適的data-action屬性,就可以觸發(fā)復(fù)制。
這個例子里,要注意一點,點擊鏈接會使瀏覽器追蹤a標(biāo)簽內(nèi)的href屬性跳轉(zhuǎn),可以取消這種默認(rèn)行為,只需要在action中調(diào)用 event.preventDefault()就可以了。
copy(event) {
event.preventDefault()
navigator.clipboard.writeText(this.sourceTarget.value)
}
還有另外一個方法,拷貝粘貼板上
copy(event) {
event.preventDefault()
this.sourceTarget.select()
document.execCommand("copy")
}
在本文中,我們看了一個在現(xiàn)實中把瀏覽器API包裝在Stimulus的controller中的例子。還有一個controller的多個實例如何同時出現(xiàn)在頁面上,我們還探索了actions和targets如何保持HTML和JavaScript的松散耦合。
下一篇文章,我們將優(yōu)化一下這個復(fù)制粘貼板的功能,讓它運行起來更加健壯。
Stimulus:瀏覽器不支持復(fù)制或者弱網(wǎng)條件下,怎么辦?
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。