jquery中擷取id值方法小結

來源:互聯網
上載者:User

複製代碼 代碼如下:
<div id="product_shift_out_{m}"> </div>
<script language = "JavaScript" type="text/javascript">
$(document).ready(function(){
name = $('div').eq(0).attr('id');
alert(name)
});
</script>

eq(0)是取第一個jq元素。。。

eq(index)
匹配一個給定索引值的元素

Matches a single element by its index.
傳回值
Element

參數
index (Number) : 從 0 開始計數

樣本
尋找第二行

HTML 程式碼:

複製代碼 代碼如下:
<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>

jQuery 代碼:

複製代碼 代碼如下:
$("tr:eq(1)")

結果:

複製代碼 代碼如下:
[ <tr><td>Value 1</td></tr> ]

擷取不同id的值

複製代碼 代碼如下:

<script src="js/jquery.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
var len = $("#group span").size();//擷取span標籤的個數
var arr = [];
for(var index = 0; index < len-1; index++){//建立一個數字數組
arr[index] = index;
}
$.each(arr, function(i){//迴圈得到不同的id的值
var idValue = $("#group span").eq(i).attr("id");
if(idValue != ''){
alert(idValue);
}
});
});
//-->
</script>
<span id="group">
<span id="0_1">aaa,
<span group_id="0_1" class="icon_close"> </span>
</span>
<span id="0_2">bbb,
<span group_id="0_2" class="icon_close"> </span>
</span>
<span id="0_3">ccc,
<span group_id="0_3" class="icon_close"> </span>
</span>
<span id="0_4">ddd,
<span group_id="0_4" class="icon_close"> </span>
</span>
<span id="0_5">eee,
<span group_id="0_5" class="icon_close"> </span>
</span>
</span>

這樣就會得到你想要的所有的id:
0_1
0_2
0_3
0_4
0_5

文字框,文本地區:

複製代碼 代碼如下:
$("#txt").attr("value",'');//清空內容
$("#txt").attr("value",'11');//填充內容

多選框checkbox:

複製代碼 代碼如下:
$("#chk1").attr("checked",'');//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined) //判斷是否已經打勾

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.