導航:首頁 > 編程語言 > js批量設置

js批量設置

發布時間:2025-01-24 18:02:37

『壹』 js中如何為許多DOM對象批量添加事件

window.onload=function(){
vardivs=document.getElementsByTagName("div");
for(vari=0;i<divs.length;i++){
if(!!divs[i].id.match(/hover/)){
divs[i].onclick=function(){console.info("spiderman!");};
}
}
}

『貳』 使用js如何給標簽的class值批量賦值

添加多個class 其實可以先從添加一個class然後對其進行封裝

好的我廢話就不多說了下面給出一份演示

<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>Title</title>
<style>
.asdasd{
width:100px;
height:100px;
background-color:#cccccc;
}
.hello{
background-color:#00AEEF;
}
</style>
</head>
<body>
<divclass="asdasd"id="demo">

</div>
<script>
//清楚空格
functiontrim(str,t){
if(typeoft=="undefined"){
t="lr";
}
switch(t){
case"l":{
returnstr.replace(/(^s*)/g,"");
}
case"r":{
returnstr.replace(/(s*$)/g,"");
}
case"lr":{
returnstr.replace(/(^s*)|(s*$)/g,"");
}
}

}
//增加Class
functionaddClass(ele,cls){
if(!ele.getAttribute)return;
varallClass=[];
cls=trim(cls);
if(ele.getAttribute("class")==null){}else{
allClass=ele.getAttribute("class").split("");
}
if(!inArr(allClass,cls)){
allClass.push(cls);
ele.setAttribute("class",allClass.join(""));
}
}
//循環
functioneach(arr,f){
if(!arr.length)returnconsole.log("error:arrayisnotlength");
for(vari=0;i<arr.length;i++){
f.call(arr[i],i,arr[i]);
}
}
//是否在數組裡面其實可以通過indexof判斷但是indexof是匹配了數據類型的
functioninArr(arr,v){
if(!arr.length)returnfalse;
for(vari=0;i<arr.length;i++){
if(v==arr[i]){returntrue;}
}
returnfalse;
}
//增加多個Class
functionaddClassArr(ele,arr){
each(arr,function(){
addClass(ele,this);
})
}
addClassArr(document.getElementById("demo"),["a1","hello","world"])
</script>
</body>
</html>

可以看到 如果沒有執行addClassArr這個函數顯示的效果的這樣的

『叄』 我想用js或者jquery 批量替換一個字元串

varstr='<divclass="demo1"style="height:auto;min-height:0px;width:1500px;color:red;「>內容1</div>'+
'<divclass="demo1"style="height:auto;min-height:0px;width:1500px;color:red;「>內容2</div>'+
<divclass="demo2"style="height:auto;min-height:0px;width:1500px;color:red;「>內容3</div>'
str=str.replace(/width:s*(d)+/g,'width:1000');
這樣後str就是替內換後的結果容

『肆』 批量修改JS文件名稱.

for i=1089914 to 1090026
Name cstr(i) & ".js" as format(i-1089913,"000") & ".txt"
next

閱讀全文

與js批量設置相關的資料

熱點內容
蘋果電腦安全軟體 瀏覽:195
u深度取消贊助密碼 瀏覽:168
編程寫東西都寫得是什麼 瀏覽:35
蔚來怎麼推薦app 瀏覽:269
6s輸入密碼來使用siri 瀏覽:742
程序員黑客頭像酷 瀏覽:739
mvc接受上傳文件 瀏覽:365
程序員可以轉金融嗎 瀏覽:193
無需用戶名密碼的共享 瀏覽:775
一加怎麼和win10傳輸文件 瀏覽:425
大數據分析哪些行業 瀏覽:848
win10刪除自動更新文件在哪裡 瀏覽:581
cad打開一個文件要關閉另一文件 瀏覽:432
鋼鐵雄心4升級包 瀏覽:558
一加找不到恢復文件 瀏覽:960
遼寧高中查分數的app有哪些 瀏覽:790
考研復試編程題可以考什麼 瀏覽:118
微信老男人頭像圖片 瀏覽:588
貸款app有哪些東西 瀏覽:74
sim卡突然無網路連接怎麼辦 瀏覽:150

友情鏈接