js 动态添加和删除样式 可以加一个id 或者 直接给个style 我是这样写的setAttribute("style","outline:"+color+" dashed 2px;") 但不知道要怎么 点击按钮时把样式删除。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!DOCTYPE HTML><html> <head> <meta charset="gb2312" /> <title></title> </head> <body> <div id="test" style="border:1px solid red;">123</div> <button id="btn">click</button> <script> function $(el){ return typeof el == 'string' ? document.getElementById(el) : el; } function $t(name, cot){ cot = cot || document; return cot.getElementsByTagName(name); } $('btn').onclick = function(){ $('test').removeAttribute('style'); } </script> </body></html>这个意思? <span id="demo">DEMO</span><script type="text/javascript">document.getElementById('demo').style.outline = '2px dashed red'; //添加document.getElementById('demo').style.outline = ''; //删除 这样不行哦 。 如果style中有别的样式那不是都给删了吗? <!DOCTYPE HTML><html> <head> <meta charset="gb2312" /> <title></title> </head> <body> <div id="test" style="border:1px solid red; color:red; font-size:123px;">123</div> <button id="btn">click</button> <script> function $(el){ return typeof el == 'string' ? document.getElementById(el) : el; } function removeStyle(obj, name){ if(!name) { obj.removeAttribute('style'); return obj; } var s = obj.style; if(s.removeAttribute){ s.removeAttribute(name); } else { name = name.replace(/([A-Z])/g, function(v){ return '-' + v.toLowerCase(); }); s.removeProperty(name); } return obj; } $('btn').onclick = function(){ removeStyle($('test'), 'fontSize') } </script> </body></html>楼主这个 意思?ie下 删除border不友好,可以 换成 对class进行操作,类似于addClass removeClass 楼主再看看。 calmcrime 在问个问题 <a class="zheng-ze-biao-da-shi ss" href="http://paper.wenweipo.com/2012/05/16/CH1205160004.htm" tag="reg">sdfsdf</a> 我想把zheng-ze-biao-da-shi ss 中的”zheng-ze-biao-da-shi空格“去掉。一个的我回两个的怎么弄。 将className的值 转换为 数组 再操作<!DOCTYPE HTML><html> <head> <meta charset="gb2312" /> <title></title> </head> <body> <a class="zheng-ze-biao-da-shi ss" href="#" tag="reg" id="test">sdfsdf</a> <script> function $(el){ return typeof el == 'string' ? document.getElementById(el) : el; } $('test').className = $('test').className.split(' ')[1]; </script> </body></html> 真是想拥有一款实用的js调试器来提高工作效率 讨论一下:利用for in循环遍历对象及子对象的属性 做了一个3张图片切换的网页,现在还要点击每张图片都可以弹出不同的页面,怎么做呢?谢谢! window.onscroll实现悬浮窗口 这个方法如何调用 急~~~ 判断 iframe 是否加载完成的完美方法??? 正则替换--请朋友帮忙 有没有人研究过joost的分层效果是如何实现的? 如何创建多维数组? 谷歌execCommand复制不到内容 我想修改一个jqeruy插件jeditable,但是有的地方不是很理解,请教一下大家。 JS能调用vba对象的事件吗
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
</head>
<body>
<div id="test" style="border:1px solid red;">123</div>
<button id="btn">click</button>
<script>
function $(el){
return typeof el == 'string' ? document.getElementById(el) : el;
}
function $t(name, cot){
cot = cot || document;
return cot.getElementsByTagName(name);
}
$('btn').onclick = function(){
$('test').removeAttribute('style');
}
</script>
</body>
</html>
这个意思?
<script type="text/javascript">
document.getElementById('demo').style.outline = '2px dashed red'; //添加
document.getElementById('demo').style.outline = ''; //删除
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
</head>
<body>
<div id="test" style="border:1px solid red; color:red; font-size:123px;">123</div>
<button id="btn">click</button>
<script>
function $(el){
return typeof el == 'string' ? document.getElementById(el) : el;
}
function removeStyle(obj, name){
if(!name) {
obj.removeAttribute('style');
return obj;
}
var s = obj.style;
if(s.removeAttribute){
s.removeAttribute(name);
} else {
name = name.replace(/([A-Z])/g, function(v){
return '-' + v.toLowerCase();
});
s.removeProperty(name);
}
return obj;
}
$('btn').onclick = function(){
removeStyle($('test'), 'fontSize')
}
</script>
</body>
</html>楼主这个 意思?
ie下 删除border不友好,
可以 换成 对class进行操作,类似于addClass removeClass 楼主再看看。
我想把zheng-ze-biao-da-shi ss 中的”zheng-ze-biao-da-shi空格“去掉。一个的我回两个的怎么弄。
<html>
<head>
<meta charset="gb2312" />
<title></title>
</head>
<body>
<a class="zheng-ze-biao-da-shi ss" href="#" tag="reg" id="test">sdfsdf</a>
<script>
function $(el){
return typeof el == 'string' ? document.getElementById(el) : el;
}
$('test').className = $('test').className.split(' ')[1];
</script>
</body>
</html>