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

熱點內容
maya粒子表達式教程 瀏覽:84
抖音小視頻如何掛app 瀏覽:283
cad怎麼設置替補文件 瀏覽:790
win10啟動文件是空的 瀏覽:397
jk網站有哪些 瀏覽:134
學編程和3d哪個更好 瀏覽:932
win10移動硬碟文件無法打開 瀏覽:385
文件名是亂碼還刪不掉 瀏覽:643
蘋果鍵盤怎麼打開任務管理器 瀏覽:437
手機桌面文件名字大全 瀏覽:334
tplink默認無線密碼是多少 瀏覽:33
ipaddgm文件 瀏覽:99
lua語言編程用哪個平台 瀏覽:272
政采雲如何導出pdf投標文件 瀏覽:529
php獲取postjson數據 瀏覽:551
javatimetask 瀏覽:16
編程的話要什麼證件 瀏覽:94
錢脈通微信多開 瀏覽:878
中學生學編程哪個培訓機構好 瀏覽:852
榮耀路由TV設置文件共享錯誤 瀏覽:525

友情鏈接