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

熱點內容
數據挖掘能實現什麼 瀏覽:686
e420硬體升級 瀏覽:225
QQ域名防紅代碼 瀏覽:756
viahdwin10 瀏覽:448
360軟體小助手怎麼添加文件夾 瀏覽:401
電視內置網路有什麼好處 瀏覽:81
微信10分鍾下款口子 瀏覽:62
中國買賣書畫的網站有哪個 瀏覽:648
vb程序最小公倍數 瀏覽:747
迅雷雲盤下載的文件怎麼找不到 瀏覽:49
樹莓派做win10電視盒子 瀏覽:690
模仁編程是什麼意思 瀏覽:267
分數app 瀏覽:135
硬碟格式化文件系統 瀏覽:325
如果禁用分頁文件 瀏覽:80
公司級的路由器密碼設置 瀏覽:581
京東app的好物榜怎麼進 瀏覽:803
word打開臨時文件夾 瀏覽:686
如何復制選中的數據 瀏覽:96

友情鏈接