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

熱點內容
百度網盤來自分享的文件在哪裡 瀏覽:127
如何發視頻到騰訊視頻文件夾里 瀏覽:510
微信打開cad外部文件找不到 瀏覽:882
平板可以打開各種文件後綴的軟體 瀏覽:531
蘋果微信文件怎麼全選 瀏覽:749
手機里之前打開過的文件在哪裡找 瀏覽:685
cad能看word文件嗎 瀏覽:719
12306火車票系統後台資料庫 瀏覽:570
js翻譯德語 瀏覽:33
從哪裡可以下載a股的數據 瀏覽:437
邏輯文件名和物理文件名關系 瀏覽:66
怎麼查一個網站的外鏈 瀏覽:675
linux下db2安裝時版本選擇 瀏覽:738
匯編編程用哪個軟體 瀏覽:486
仙樂下的歌在文件管理哪裡 瀏覽:477
115網盤解析工具2014 瀏覽:371
內圓弧銑刀怎麼編程 瀏覽:410
記事本文件轉word格式對齊 瀏覽:300
excel刪除恢復文件 瀏覽:290
三星s4怎麼切換3g網路 瀏覽:994

友情鏈接