日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

jQuery?表單事件與遍歷詳情_jquery

作者:RiemannHypothesis??????? ? 更新時間: 2022-10-26 編程語言

表單事件

.blur()

為 "blur" 事件綁定一個處理函數(shù),或者觸發(fā)元素上的 "blur" 事件(注:此事件不支持冒泡)。

$('#other').click(function() {
  $('#target').blur();
});

.focus()

為 JavaScript 的 "focus" 事件綁定一個處理函數(shù),或者觸發(fā)元素上的 "focus" 事件。

$('#target').focus(function() {
  alert('Handler for .focus() called.');
});

.change()

為JavaScript 的 "change" 事件綁定一個處理函數(shù),或者觸發(fā)元素上的 "change" 事件。

$('.target').change(function() {
  alert('Handler for .change() called.');
});

.submit()

當(dāng)用戶試圖提交表單時,就會在這個表單元素上觸發(fā)submit事件。它只能綁定在<form>元素上。

$('#target').submit(function() {
  alert('Handler for .submit() called.');
});

遍歷

.map()

通過一個函數(shù)匹配當(dāng)前集合中的每個元素,產(chǎn)生一個包含新的jQuery對象。

由于返回值是一個jQuery包裹的數(shù)組,所以通常會使用get()方法將其轉(zhuǎn)換成普通的數(shù)組。

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="./jquery-3.6.0.min.js" charset="utf-8"></script>
  </head>
  <body>
    <form method="post" action="">
       <fieldset>
         <div>
           <label for="two">2</label>
           <input type="checkbox" value="2" id="two" name="number[]">
         </div>
         <div>
           <label for="four">4</label>
           <input type="checkbox" value="4" id="four" name="number[]">
         </div>
         <div>
           <label for="six">6</label>
           <input type="checkbox" value="6" id="six" name="number[]">
         </div>
         <div>
           <label for="eight">8</label>
           <input type="checkbox" value="8" id="eight" name="number[]">
         </div>
       </fieldset>
      </form>
     <script type="text/javascript">
       $('input').map(function(index) {
         console.log(this.id);
       })
     </script>
  </body>
</html>

map()方法會返回一個新的數(shù)組。

.each()

遍歷一個jQuery對象,為每個匹配元素執(zhí)行一個函數(shù)。

<ul>
    <li>foo</li>
    <li>bar</li>
</ul>
$( "li" ).each(function( index ) {
  console.log( index + ":" + $(this).text());
});

each()返回的是原來的數(shù)組,并不會新創(chuàng)建一個數(shù)組。

.get()

通過jQuery對象獲取一個對應(yīng)的DOM元素。

<ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
</ul>
console.log( $( "li" ).get( 0 ) );

原文鏈接:https://juejin.cn/post/7137702260326268942

欄目分類
最近更新