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

热点内容
联想启天m7300能升级内存吗 浏览:698
精雕怎么打开图片文件 浏览:161
ug编程镗孔g76如何使用 浏览:719
7岁到18岁少儿编程学什么 浏览:413
mac文件怎么打印 浏览:756
农药登记许可证查询哪个网站 浏览:857
素材站用什么网站系统 浏览:173
ug如何用铰刀编程 浏览:647
三国志版本介绍 浏览:260
情侣用的定位app有哪些 浏览:244
怎么清楚苹果手机的健身数据 浏览:988
淘宝美工教程自学网 浏览:452
父子2006未删减版本 浏览:872
包头谁卖苹果6 浏览:504
化工企业停工有哪些文件通知要求 浏览:878
下载软件后文件夹在桌面 浏览:397
word文字编组 浏览:167
旅游策划的程序 浏览:519
哪些app能看象棋直播 浏览:819
小米4怎么传文件 浏览:210

友情链接