本文目錄一覽:
請求一段簡單的js代碼。謝謝
script type=”text/javascript”
var arr = [1,3,6,2,7,8,2,5,7,8,0];
var count2 = 0;
var count7 = 0;
for(var i = 0;iarr.length;i++){
if(arr[i] === 2){
console.log(“2出現在第”+i+”個”);
count2++;
}
if(arr[i] === 7){
console.log(“7出現在第”+i+”個”);
count7++;
}
}
console.log(“2出現了”+count2+”次”);
console.log(“7出現了”+count7+”次”);
/script
簡單的JS代碼
我對JS不是很了解,但是看了一下.覺得你上移和下移那點的參數是不是有問題呀?
input type=”button” name=”moveUP” value=”上移一行” onClick=”moveUp(this);”/
input type=”button” name=”moveDOWN” value=”下移一行” onClick=”moveDown(this);”/
這裡傳this作為參數的話不就是把按鈕傳進去了嗎?
在方法裡面是不是應該先得到選中的單選按鈕,然後通過DOM對象來得到行的索引呀?
嘿嘿,我是菜鳥,不對還望多多指教.
我給你改了一下,貌似可以了代碼如下:
html
head
script language=”javascript”
//行的追加
function addRow() {
var testTable = document.getElementById(“testTable”);
var bodies = testTable.tBodies;
var aBody = null;
if(bodies){
aBody = bodies[0];
}
if(aBody){
var row = document.createElement(“tr”);
for(var i = 0 ; i testTable.tHead.rows[0].cells.length; i++){
var cell = document.createElement(“td”);
var str = “內容第” + (aBody.rows.length + 1) + “行第” + (i + 1) + “列”;
if(i == (testTable.tHead.rows[0].cells.length – 1)) {
str = ” a href=’javascript:void(0);’ onclick=\”removeRow(this);\”刪除/a”;
}else if(i == 0){
str = “input type=\”radio\” name=\”RAd\” “;
} cell.innerHTML = str;
row.appendChild(cell);
}
aBody.insertBefore(row);
}
}
//行的刪除
function removeRow(obj) {
var testTable = document.getElementById(“testTable”);
var bodies = testTable.tBodies;
var aBody = null;
if(bodies){
aBody = bodies[0];
if(aBody){
aBody.removeChild(obj.parentNode.parentNode);
}
}
}
//行的上移
function moveUp(src){
var rowIndex = 0;
var rad = document.getElementsByName(“RAd”);
for(var i = 0; i rad.length; i++){
if(rad[i].checked){
rowIndex = rad[i].parentElement.parentElement.rowIndex;
}
}
if (rowIndex = 2){
change_row(rowIndex-1,rowIndex);
}
}
//行的下移
function moveDown(src){
var rowIndex = 0;
var rad = document.getElementsByName(“RAd”);
for(var i = 0; i rad.length; i++){
if(rad[i].checked){
rowIndex = rad[i].parentElement.parentElement.rowIndex;
}
}
var tl = document.getElementById(“testTable”);
if (rowIndex tl.rows.length – 1){
change_row(rowIndex + 1,rowIndex);
}
}
function change_row(line1, line2){
var tl = document.getElementById(“testTable”);
tl.rows[line1].swapNode(tl.rows[line2]);
}
/script
/head
body
div
table id=”testTable” border=”1″ width=”80%”
thead
tr
th scope=”col”單選按鈕/th
th scope=”col”序列/th
th scope=”col”ID/th
th scope=”col”名字/th
/tr
/thead
/table
input type=”button” name=”addButton” value=”追加一行” onClick=”addRow();”/
input type=”button” name=”moveUP” value=”上移一行” onClick=”moveUp(this);”/
input type=”button” name=”moveDOWN” value=”下移一行” onClick=”moveDown(this);”/
/div
/body
/html
求一個簡單的點擊圖片放大縮小的JS代碼
1、準備好需要用到的圖標。
2、新建html文檔。
3、書寫hmtl代碼。div id=allbox div id=boxhome img style=”WIDTH: 107px; BOTTOM: 5px; HEIGHT: 176px; LEFT: 10px” id=imgSmallLeft class=imgBorder onClick=”clearInterval(autoplay);moveD(‘l’);” 。
4、書寫並添加js代碼。script src=”js/ntes_jslib_1.x.js”/scriptscript src=”js/zzsc.js”/script。
5、代碼整體結構。
6、查看效果。
求大神給寫一個最簡單的js代碼!
div id=”div1″111/div
div id=”div2″222/div
style
#div1 , #div2{
background: red;
width: 100px;
margin: 10px;
}
/style
script
window.onload = function(){
var btn1 = document.getElementById(“div1”);
var btn2 = document.getElementById(“div2”);
btn1.onclick = function(){
btn2.style.background = “yellow”;
}
}
/script
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/271857.html