我有一个元素,从元素我怎么知道它是否应用了inline
样式?
示例:
html:
<span style="border:1px solid red;">Testing</span>
//<span>Testing</span> //without style as well getting some in build info
js:
var sty = $('span').prop('style');
console.log(sty); //it always consoles something!
答案 0 :(得分:2)
var sty = $('span').is('[style]');
if(sty)
{
//style is present
}
else
{
//style is not present
}
<强> Demo 强>
答案 1 :(得分:0)
if ($('span').hasAttr("style")) {
console.log("element 'span' has a style attribute");
}
编辑:我很匆忙忘了添加hasAttr
功能,抱歉:)
$.fn.hasAttr = function(){
if (this.attr('style') !== "undefined") {
return false;
} else {
return true;
}
}
说实话,你可以使用if($("span").attr('style') !== undefined)
答案 2 :(得分:0)
试试这个: -
if(typeof($('span').attr('style'))==typeof('undefined') && $('span').attr('style')==undefined)