導航:首頁 > 編程語言 > 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點擊次數的奇偶數判斷代碼相關的資料

熱點內容
任命文件人字多少號 瀏覽:27
如何給桌面的文件自動填寫序號 瀏覽:349
壓縮文件JPG重命名 瀏覽:487
vivox7怎麼隱藏文件 瀏覽:340
官網買蘋果好處 瀏覽:153
直播app源碼如何搭建 瀏覽:31
c盤系統文件包介紹圖片 瀏覽:856
linuxopt主要存哪些文件 瀏覽:924
jsdate天數 瀏覽:930
qq空間怎樣發視頻文件在哪裡找 瀏覽:208
nrf2401收發程序 瀏覽:498
哪個網站看新聞最好 瀏覽:944
找到文件夾並清除linux 瀏覽:82
蘋果app不安全怎麼辦 瀏覽:1000
電腦桌面文件收集軟體推薦 瀏覽:317
桌面的文件夾不能刪除不了怎麼辦 瀏覽:724
什麼是手機溫控文件 瀏覽:840
廣東農信app怎麼更改預留手機號碼 瀏覽:241
cdm打開隱藏文件夾 瀏覽:320
貝殼app房子為什麼偶爾不顯示 瀏覽:4

友情鏈接