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

热点内容
windowsxp中工具菜单 浏览:663
43岁学编程怎么样 浏览:721
oracle如何清空表数据 浏览:856
lifi网络如何 浏览:744
如何利用jspwebser 浏览:188
在线调试jses6 浏览:834
javascript实现指定行列的表格 浏览:55
安装驱动程序下载 浏览:660
小米平板共享电脑文件夹在哪里 浏览:977
免费的电脑文件打印软件 浏览:262
pdf文件名长度有限制吗 浏览:813
暗黑破坏神2atma505图文教程 浏览:373
苹果手机怎么文件形式重命名 浏览:83
沈阳如何用app办理个人养老 浏览:698
ps键盘教程 浏览:145
四个excel文件怎么合并 浏览:477
如何将手机视频导入文件管理 浏览:381
cfile读取多个文件 浏览:138
存放的文件有哪些 浏览:582
机器人编程课推广文案怎么写 浏览:950

友情链接