找到我的元素已应用`inline`样式

时间:2015-01-24 08:44:35

标签: jquery

我有一个元素,从元素我怎么知道它是否应用了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!

Jsfiddle

3 个答案:

答案 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)