导航:首页 > 编程语言 > 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

友情链接