Jquery操作DOM - 解析HTML元素

來源:互聯網
上載者:User

標籤:

基本操作{DOM}
-html()方法 類似原於DOM中innerHTML
 擷取       html()
 設定(修改) html(html)

 1   <button>0</button> 2  3   <script src="js/jquery-1.11.3.js"></script> 4   <script> 5     $(‘button‘).click(function(){ 6         var intValue = parseInt($(this).html()); 7         intValue++; 8         $(this).html(intValue); 9     })10   </script>

 

-val() 方法 類似原於DOM中value
 擷取       val()
 設定(修改) val(value值)

 1   <h3>我的購物車</h3> 2   單價:<span id="price">3.5</span> 3   數量: 4     <button id="btLess">-</button><input name="count" value="1" style="width:30px"><button id="btMore">+</button> 5   小計: 6     <input name="total" value="3.5" disabled> 7  8   <hr> 9   使用者姓名:10   <input name="userName"><br>11   使用者性別:12   <input type="radio" name="userSex" value="male">男13   <input type="radio" name="userSex" value="female" checked>女14   <br>15   所在城市:16   <select name="city">17     <option value="bj">北京市</option>18     <option value="sh">上海市</option>19     <option value="zj">浙江省</option>20   </select><br>21   興趣愛好:22   <input type="checkbox" name="hobby" value="xy">洗衣23   <input type="checkbox" name="hobby" value="zf">做飯24   <input type="checkbox" name="hobby" value="sd">掃地<br>25 26   <input type="button" value="提交訂單" id="btSubmit">27 28 29 30   <script src="js/jquery-1.11.3.js"></script>31   <script>32   $(‘#btLess‘).click(function(){33     var count = $(‘[name="count"]‘).val();34     count = parseInt(count);35     count--;36     $(‘[name="count"]‘).val(count);37     //計算小計38     var price = $(‘#price‘).html();39     $(‘[name="total"]‘).val( price*count );40   })41   $(‘#btMore‘).click(function(){42     var count = $(‘[name="count"]‘).val();43     count = parseInt(count);44     count++;45     $(‘[name="count"]‘).val(count);46     //計算小計47     var price = $(‘#price‘).html();48     $(‘[name="total"]‘).val( price*count );49   })50 51   $(‘#btSubmit‘).click(function(){52     //收集使用者的輸入53     var n = $(‘[name="userName"]‘).val();54     var s = $(‘[name="userSex"]:checked‘).val();55     //var c = $(‘option:selected‘).val();56     var c = $(‘select[name="city"]‘).val();57     console.log(n+s+c);58 59     //注意複選框多個值的擷取60     //var h = $(‘[name="hobby"]:checked‘).val();61     var jqObj = $(‘[name="hobby"]:checked‘);62     for(var i=0; i<jqObj.length; i++){63       var cb = jqObj[i];64       var h = $(cb).val();65       console.log(h);66     }67   });68   </script>

-attr()方法 類似原於DOM中getAttribute();
                         setAttribute();
 擷取       attr(屬性名稱)
 設定(修改) attr(屬性名稱,屬性值)

 1   <img class="thumbnail" src="img/1.jpg" alt="img/1111.jpg"> 2   <img class="thumbnail" src="img/2.jpg" alt="img/2222.jpg"> 3   <img class="thumbnail" src="img/3.jpg" alt="img/3333.jpg"> 4   <img class="thumbnail" src="img/4.jpg" alt="img/4444.jpg"> 5  6   <hr> 7  8   <img class="full-img" src="img/1111.jpg"> 9 10   <script src="js/jquery-1.11.3.js"></script>11   <script>12   $(‘.thumbnail‘).mouseenter(function(){13         var alt = $(this).attr(‘alt‘);14         $(‘.full-img‘).attr(‘src‘,alt);15   });16   </script>

 

                        

Jquery操作DOM - 解析HTML元素

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.