導航:首頁 > 編程語言 > js定義button

js定義button

發布時間:2024-08-24 17:57:24

js里怎麼用button按鈕來控制一個div里若干li的選擇

先引入jquery,節約代碼

css代碼

.selected{background-color:#ff0000}

html代碼

<buttonid="chooseNext"></button>
<divid="liList">
<liclass="selected">1111111</li>
<li>2222222</li>
<li>3333333</li>
<li>4444444</li>
<li>5555555</li>
<li>6666666</li>
<li>7777777</li>
</div>

js代碼

	$(function(){
varlength=$("#liList").children("li").length-1;
$("#chooseNext").on("click",function(){
varsIndex=$("#liList").children("li.selected").index();
if(sIndex==length){
sIndex=-1;
}
$("#liList").children().removeClass("selected");
$("#liList").children("li:eq("+(sIndex+1)+")").addClass("selected");
})
})

完整代碼

<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8">
<title></title>
<scriptsrc="js/jquery-1.11.2.min.js"type="text/javascript"charset="utf-8"></script>
</head>
<body>
<styletype="text/css">
.selected{background-color:#ff0000}
</style>
<buttonid="chooseNext">選擇下一個</button>
<divid="liList">
<liclass="selected">1111111</li>
<li>2222222</li>
<li>3333333</li>
<li>4444444</li>
<li>5555555</li>
<li>6666666</li>
<li>7777777</li>
</div>
<scripttype="text/javascript">
$(function(){
varlength=$("#liList").children("li").length-1;
$("#chooseNext").on("click",function(){
varsIndex=$("#liList").children("li.selected").index();
if(sIndex==length){
sIndex=-1;
}
$("#liList").children().removeClass("selected");
$("#liList").children("li:eq("+(sIndex+1)+")").addClass("selected");
})
})
</script>


</body>
</html>

② js如何設置動態按鈕 就是一個button,有value值,點一下,value的值改變,再點一下,value的值恢復原值!

用jquery的處理方式:

var b = 0;
$("#buttonId").click(function(){
if (b == 0)
{
b = 1;
$("#buttonId").val("確定");
}
else
{
b = 0;
$("#buttonId").val("取消");
}
});

③ 在javascript中怎麼設置button的可點擊和不可點擊

、js中設置按陵則跡鈕可點擊與不可尺並點擊,默認是可點擊的

(1)設置按鈕不可點擊

document.getElementById("bt1").disabled=ture;

(2)設置按鈕可點擊

document.getElementById("bt1").disabled=false;

2、jq中設置按鈕可點擊與不可點擊,默認是可點擊的盯遲

(1)設置按鈕不可點擊

$("#bt1").attr("disabled",ture);

(1)設置按鈕可點擊

$("#bt1").attr("disabled",false);

3、標簽中設置按鈕不可點擊

在標簽中添加屬性disabled="true"。

④ js怎樣實現button點擊它會被選中,再次點擊取消選中

button{
background:#fff;
}
button.active{
background:red;
}

假設上邊的css表示button的倆種狀態,正常狀態和選中狀態

<buttontype='button'id="btn">按鈕</button>

這個是按鈕


varbtn=document.getElementById('btn');
btn.onclick=function(){//添加點擊事件
if(btn.className.indexOf('active')){//說明已經是選中狀態
btn.className='';//清空class。
}else{
btn.className='active';//否則選中它,給它添加active樣式
}
}


//如果用jquery,會很方便實現
$('#btn').click(function(){//給id為btn的元素添加點擊事件
$(this).toggleClass('active');//每次點擊的時候,將當前的元素切換active樣式
//如果有,則去掉,否則添加
});

⑤ JS如何控制button的位置

解決方法:

1、把button定義成絕對定位,position:absoulte的方式,然後設置left,top的方式進行位置控制

2、如果是節點移動,則可以通過dom刪除和增加的方式來調整位置

問題解決:

這里針對的是第二種情況,可以把對應的節點獲取後,刪除再插入到對應的節點後。

代碼示例:

<script>
functionmove(self){
varp=self.parentNode;//獲取當前節點的父節點
self.remove();//移除當前節點
p.appendChild(self);//父節點添加當前節點
}
</script>
</head>
<body>
<div>
<inputtype="button"id="button1"value="1"onclick="move(this)">
<inputtype="button"id="button2"value="2"/>
</div>
</body>
閱讀全文

與js定義button相關的資料

熱點內容
php從資料庫讀取數據 瀏覽:330
蘋果手機安裝描述文件應用下架 瀏覽:480
安全程序文件包含哪些 瀏覽:612
ibatis查詢資料庫 瀏覽:453
手機卸載文件找不到文件怎麼辦 瀏覽:545
百度網盤app下載的壓縮文件怎麼導出來 瀏覽:254
小米5開發版驅動程序 瀏覽:785
微信附近搜索怎樣打開 瀏覽:543
ios7那個版本越獄好 瀏覽:344
壓縮文件如何分割 瀏覽:458
蘋果4s怎麼沒地方插卡 瀏覽:943
資料庫的組成對象11 瀏覽:944
java字元串分段 瀏覽:545
蘋果6為什麼只能用2g 瀏覽:471
編程的第一天是什麼 瀏覽:161
jsp3個靜態指令 瀏覽:211
lgv20韓版升級 瀏覽:68
su找不到許可文件 瀏覽:211
java反射構造方法 瀏覽:968
Win10系統開機讀寫文件錯誤 瀏覽:89

友情鏈接