導航:首頁 > 編程語言 > 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相關的資料

熱點內容
專題學習網站源碼 瀏覽:163
jsphead什麼 瀏覽:88
gps串口數據怎麼發送 瀏覽:968
win10文件主頁共享查看 瀏覽:411
中國聯通有哪些app是免流的 瀏覽:176
邊做邊保存的文件找不到了 瀏覽:858
win10照片應用文件夾名稱 瀏覽:966
編程如何解決資金的原子性 瀏覽:638
如何製作廣角鏡頭矯正文件 瀏覽:513
在網頁開發中應該選用哪個資料庫 瀏覽:742
iphone5移動卡貼 瀏覽:990
電腦文件的格式 瀏覽:127
extjs的xtype 瀏覽:959
suse11iso文件要u盤安裝 瀏覽:153
如何將報表統計數據轉化為圖形 瀏覽:444
如何寄快遞材料文件 瀏覽:265
java構造方法private 瀏覽:475
手機文件找回恢復 瀏覽:516
word怎麼把u盤里的文件拔掉 瀏覽:976
港版蘋果用的插排 瀏覽:1000

友情鏈接