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

熱點內容
企業微信素材庫文件下載鏈接 瀏覽:582
pdf文件怎麼取消標黃 瀏覽:781
打電話時不能使用網路是什麼原因 瀏覽:919
ps怎麼將文件合並 瀏覽:747
java的日期格式化 瀏覽:981
電腦應用程序怎麼關 瀏覽:986
微信上鏈接在哪個文件夾 瀏覽:691
歐姆龍安裝打開找不到密鑰文件 瀏覽:302
蘋果基本表情 瀏覽:128
我的世界教育版編程在哪裡 瀏覽:842
pong文件夾找不到 瀏覽:759
69版本黑切 瀏覽:997
杭州道富java 瀏覽:635
知道qq號查微博賬號和密碼 瀏覽:294
紅手指自帶哪些app 瀏覽:103
手機用公司網路會被監控哪些 瀏覽:409
什麼叫py編程 瀏覽:370
微信紅包尾數作弊器ios 瀏覽:280
ipad備份文件夾 瀏覽:555
vivo手機qq下載的文件在哪裡 瀏覽:303

友情鏈接