
js 中style对象无法获取外链css属性,返回一个空,但为何还能通过style设置外链的css属性呢?
var$=function(){returnnewBase();}functionBase(){this.element=[];}//设置cssBase.prototyp...
var $ = function(){
return new Base();
}
function Base(){
this.element=[];
}
//设置css
Base.prototype.css=function(attr,value){
for(var i=0;i<this.element.length;i++){
//获取css属性
if(arguments.length == 1){
if(typeof window.getComputedStyle!='undefined'){
return window.getComputedStyle(this.element[i],null)[attr];
}else if(typeof this.element[i].currentStyle!='undefined'){
return this.element[i].currentStyle[attr];
}
}//设置css属性
this.element[i].style[attr]=value;
}
return this;
}
上面代码可以运行,问题就是style无法获取外链css样式属性,却能修改属性。 展开
return new Base();
}
function Base(){
this.element=[];
}
//设置css
Base.prototype.css=function(attr,value){
for(var i=0;i<this.element.length;i++){
//获取css属性
if(arguments.length == 1){
if(typeof window.getComputedStyle!='undefined'){
return window.getComputedStyle(this.element[i],null)[attr];
}else if(typeof this.element[i].currentStyle!='undefined'){
return this.element[i].currentStyle[attr];
}
}//设置css属性
this.element[i].style[attr]=value;
}
return this;
}
上面代码可以运行,问题就是style无法获取外链css样式属性,却能修改属性。 展开
2个回答
展开全部
JS是不能获取引用的CSS的样式属性的,无论是外链还是内链,修改某个CSS也是通过style这样的方式,比如说
<ul class="channel" style="color: #fff;">
当然上面这样的HTML 你也可以获取到 channel 这个节点的 color的属性,其他的属性就无法获取,但是你却可以修改其他任何的属性。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |