導航:首頁 > 編程語言 > js點擊次數的奇偶數判斷代碼

js點擊次數的奇偶數判斷代碼

發布時間:2023-09-05 04:14:09

javascript怎樣 獲取表格奇數行偶數行

1、遍歷tr,得到滑鼠所在tr的索引值,然後用二樓所說的方法判斷奇偶;
2、用jQuery方便很多,在選擇器後面加上":even"便選擇的是索引值為偶數的元素,加":odd"便是索引值為奇數的元素。

下面是實現的代碼,包括jQuery的:
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery/jquery-1.4.2.js"></script>
<script type="text/javascript">
function changeStyle(elementId) {
var testTable = document.getElementById("testTable").children[0];
for(var i = 0; i < testTable.children.length; i++) {
if(testTable.children[i] == elementId) {
if(i % 2 == 1) //奇數
elementId.style.background = "red";
else //偶數
elementId.style.background = "blue";
}
}
}

//清除樣式
function changeBack(elementId) {
elementId.style.background = "";
}

/**
* jQuery方法:
*/
$(document).ready(function() {
$("#jqueryTable tr:even").mouseover(function() {
$(this).css("background", "red");
});
$("#jqueryTable tr:odd").mouseover(function() {
$(this).css("background", "blue");
});
$("#jqueryTable tr").mouseout(function() {
$(this).css("background", "");
});
});
</script>
</head>
<body>
<table id="testTable" border="1">
<tr onmouseover="changeStyle(this)" onmouseout="changeBack(this)">
<td>第</td><td>一行</td>
</tr>
<tr onmouseover="changeStyle(this)" onmouseout="changeBack(this)">
<td>第</td><td>二行</td>
</tr>
<tr onmouseover="changeStyle(this)" onmouseout="changeBack(this)">
<td>第</td><td>三行</td>
</tr>
<tr onmouseover="changeStyle(this)" onmouseout="changeBack(this)">
<td>第</td><td>四行</td>
</tr>
<tr onmouseover="changeStyle(this)" onmouseout="changeBack(this)">
<td>第</td><td>五行</td>
</tr>
</table>

<table id="jqueryTable" border="1">
<tr>
<td>第一行</td>
</tr>
<tr>
<td>第二行</td>
</tr>
<tr>
<td>第三行</td>
</tr>
<tr>
<td>第四行</td>
</tr>
<tr>
<td>第五行</td>
</tr>
</table>
</body>
</html>

❷ js/jquary 點擊次數 奇偶性的判斷

1、你之前的答案是對的。有問題的是你全局變數的定義有問題,把你代碼里的專 i 變數的屬定義移到方法之外定義即可。

2、但是比較奇怪的是,你的代碼里對於i變數沒有進行奇數與偶數的處理,不知道是不是你代碼沒有貼全。

3、這個代碼邏輯是比較簡單的。具體代碼如下圖所示。

❸ 在JS中,利用if else for 循環,判斷奇數和偶數,並用不同的方法輸出

functionjishu(num)
{
console.log('我是奇數:專'+num)
}
functionoushu(num)
{
console.log('我是偶屬數:'+num)
}
for(vari=1;i<=100;i++)
{
if(i%2==0)
{
oushu(i)
}
else
{
jishu(i)
}
}

❹ 怎麼用js寫一段代碼,求一個數組中的最大奇數和最小偶數,並返回他們的和,如果一個數不存在則返回null。

functiontest(arr){
//先排序
arr.sort(function(value1,value2){
returnvalue1-value2;
});
//取出偶數和奇數數組
vareven=arr.filter(function(item){
returnitem%2==0;
})
varodd=arr.filter(function(item){
returnitem%2==1;
})
if(even.length>0&&odd.length>0){
returneven.shift()+odd.pop();
}else{
returnnull;
}
}

閱讀全文

與js點擊次數的奇偶數判斷代碼相關的資料

熱點內容
什麼app能看香港房 瀏覽:590
喪屍電視劇哪個app看 瀏覽:196
電腦版上傳視頻找不到文件在哪 瀏覽:164
javastringtemplate 瀏覽:594
聯想啟天m7300能升級內存嗎 瀏覽:698
精雕怎麼打開圖片文件 瀏覽:161
ug編程鏜孔g76如何使用 瀏覽:719
7歲到18歲少兒編程學什麼 瀏覽:413
mac文件怎麼列印 瀏覽:756
農葯登記許可證查詢哪個網站 瀏覽:857
素材站用什麼網站系統 瀏覽:173
ug如何用鉸刀編程 瀏覽:647
三國志版本介紹 瀏覽:260
情侶用的定位app有哪些 瀏覽:244
怎麼清楚蘋果手機的健身數據 瀏覽:988
淘寶美工教程自學網 瀏覽:452
父子2006未刪減版本 瀏覽:872
包頭誰賣蘋果6 瀏覽:504
化工企業停工有哪些文件通知要求 瀏覽:878
下載軟體後文件夾在桌面 瀏覽:397

友情鏈接