导航:首页 > 编程语言 > 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批量设置相关的资料

热点内容
任命文件人字多少号 浏览:27
如何给桌面的文件自动填写序号 浏览:349
压缩文件JPG重命名 浏览:487
vivox7怎么隐藏文件 浏览:340
官网买苹果好处 浏览:153
直播app源码如何搭建 浏览:31
c盘系统文件包介绍图片 浏览:856
linuxopt主要存哪些文件 浏览:924
jsdate天数 浏览:930
qq空间怎样发视频文件在哪里找 浏览:208
nrf2401收发程序 浏览:498
哪个网站看新闻最好 浏览:944
找到文件夹并清除linux 浏览:82
苹果app不安全怎么办 浏览:1000
电脑桌面文件收集软件推荐 浏览:317
桌面的文件夹不能删除不了怎么办 浏览:724
什么是手机温控文件 浏览:840
广东农信app怎么更改预留手机号码 浏览:241
cdm打开隐藏文件夹 浏览:320
贝壳app房子为什么偶尔不显示 浏览:4

友情链接