本文實(shí)例講述了JQuery屬性操作與循環(huán)用法。分享給大家供大家參考,具體如下:
取出或者設(shè)置html內(nèi)容
var $htm= $("#div").html
取出$("#div").html("<span>文字</span>")
設(shè)置
取出或者設(shè)置某個(gè)屬性的值
var $src=$('#img1').prop('src')
取出$('#img1').prop({src:'test.jpg',alt:"test Image"})
設(shè)置
設(shè)置a標(biāo)簽的屬性
跳轉(zhuǎn)到百度的鏈接
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(function () { var $a=$('.link'); console.log($a.prop('class'));/*沒有設(shè)置的屬性要是讀的話會(huì)讀為空*/ $a.prop({href:'http://www.baidu.com',title:'百度'});/*設(shè)置屬性*/ }) </script></head><body> <a href="#" rel="external nofollow" class="link">這是一個(gè)a標(biāo)簽</a></body></html>
JQuery的循環(huán)
對(duì)JQuery選擇的對(duì)象集合分別進(jìn)行操作,需要用到j(luò)query循環(huán)操作,此時(shí)可以用到each
方法
$(function(){ $('.list li').each(function(i){ $(this).html(i) })})
得到list類下面的每一個(gè)li,賦值加上i。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(function () { var $li=$('.list li'); $li.each(function (index) { // $(this).css() if($(this).index()%2==0){ $(this).css({backgroundColor:'gold'}); } }) }) </script></head><body><ul class="list"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li></ul></body></html>
各行換色。
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具 http://tools.VeVB.COm/code/HtmlJsRun 測(cè)試上述代碼運(yùn)行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery常見事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注