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

熱點內容
一個視頻網站能賺多少錢 瀏覽:357
在電腦上怎麼復制文件 瀏覽:496
我的世界文件夾mod在哪裡 瀏覽:525
蘋果5s測評 瀏覽:782
安全文件夾在哪裡下 瀏覽:643
catpart文件名怎麼設置 瀏覽:39
PDF檔文件怎麼另存為 瀏覽:402
網路機頂盒恢復出廠怎麼能下載 瀏覽:4
網頁元素代碼 瀏覽:67
讀書選擇版本的原因 瀏覽:992
如何用數據線傳文件 瀏覽:335
java使用jni 瀏覽:432
p970升級錯誤修復 瀏覽:119
米4文件管理在哪 瀏覽:376
ug編程用哪個版本最經典 瀏覽:420
javamysqljdbc導入 瀏覽:365
js某個js先執行 瀏覽:508
視頻文件怎麼變成兩個了 瀏覽:523
exle表格如何匹配數據 瀏覽:850
丑角怎麼畫app 瀏覽:57

友情鏈接