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

熱點內容
學生計算機編程網課哪個好 瀏覽:126
xp系統賬戶刪除密碼忘記了 瀏覽:771
什麼是對程序進行模塊化編程 瀏覽:227
win10文件更改怎麼找回 瀏覽:83
安卓的QQ文件怎麼刪除 瀏覽:779
photoshopcs6免費教程 瀏覽:209
復雜網路理論及其應用汪小帆 瀏覽:343
不同平台數據集如何合並 瀏覽:599
網路主播是什麼軟體 瀏覽:939
thinkpad編程用什麼系列 瀏覽:172
ps照片導出電腦桌面丟失文件 瀏覽:976
好看的旅遊網站模板下載 瀏覽:72
手機里excel文件導出 瀏覽:577
holo手機文件管理器 瀏覽:198
XLS1個文件夾能存多少kB 瀏覽:703
revit2014建築設計教程 瀏覽:723
如何把圖片放到word文件中去 瀏覽:704
開通微信原創是不是每一篇文章 瀏覽:528
編程在大學什麼專業學的好 瀏覽:692
學生文件櫃多少錢 瀏覽:229

友情鏈接