Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript检查HTML样式属性是否存在_Javascript_Html_Css_Styles - Fatal编程技术网

如何使用javascript检查HTML样式属性是否存在

如何使用javascript检查HTML样式属性是否存在,javascript,html,css,styles,Javascript,Html,Css,Styles,我试图找出特定元素是否具有内联样式属性: 我相信有一个简单的方法来检查这个,但我似乎找不到它。 我已经尝试了多种方法,包括: var contentWrapper = document.getElementById("contentWrapper"); if(contentWrapper.style.display.toString=="") alert("Empty"); else alert("Not Empty"); 谢谢你的帮助 if(contentWrapper.getAttribu

我试图找出特定元素是否具有内联样式属性: 我相信有一个简单的方法来检查这个,但我似乎找不到它。 我已经尝试了多种方法,包括:

var contentWrapper = document.getElementById("contentWrapper");
if(contentWrapper.style.display.toString=="")
alert("Empty");
else
alert("Not Empty");
谢谢你的帮助

if(contentWrapper.getAttribute("style")){
    if(contentWrapper.getAttribute("style").indexOf("display:") != -1){
        alert("Not Empty");
    } else {
        alert("Empty");
    }
}

以上几行适合你(任何人都可以选择)

在第二种解决方案中:

首先检查元素中是否存在
样式属性
,第二次检查确保
样式属性
不作为
空字符串
存在,例如

完整代码如下所示:

var contentWrapper = document.getElementById("contentWrapper");
if(contentWrapper.getAttribute("style")==null || contentWrapper.getAttribute("style")=="")
alert("Empty");
else
alert("Not Empty");
(第一种解决方案)


(第二种解决方案)

我第一次扫描此页面时,没有看到@plalx的评论

if (element.hasAttribute("style"))
{
    var styleText = element.getAttribute("style")
}

在一个相关的注释中,关于样式

//to get info about the end results of CSS
var computedStyle = element.currentStyle || getComputedStyle(element, null);


style对象有一个
length
属性,它告诉您元素是否具有任何内联样式。这也避免了属性
style
存在但为空的问题

//如果没有应用样式,则为0;如果应用了内联样式,则为>0
contentWrapper.style.length
//所以你可以像这样检查它
contentWrapper.style.length==0

检查给定Id的样式属性是否存在

if(document.getElementById("idname").hasAttribute("style")){
  alert("Style attribute found");
}else{
  alert("Style attribute not found");
}

可能重复的
getAttribute('style')
此外,调用函数时要小心,确保添加括号<当您想要比较函数的返回值时,code>display.toString==“”正在比较函数。它应该是display.toString()。(这并不是说您需要
.toString()
,因为
display
已经是一个字符串,但这是代码可能失败的另一个原因)链接的两个副本在jQuery中提供了用户没有明确要求的解决方案;因此,这不是这两个问题的重复。重复检查是不必要的,因为,
null
'
都是错误的。另外,最好是。第二次检查是确保style属性不以空字符串的形式出现,例如,我知道,但我的意思是
Boolean(“”)
false
。因此,
如果(!contentWrapper.getAttribute('style')){//empty}
适用于
null
'
。这对于像“/*字体大小:24px;*/”这样的注释字符串来说并不完美。在这种情况下,它的工作原理就像字体大小存在一样。
//to get info about the end results of CSS
var computedStyle = element.currentStyle || getComputedStyle(element, null);
//to iterate over css styles from style tags or linked CSS
for i ...
    document.styleSheets[i].rules ...

//great for searching with
var elements = document.querySelectorAll(rules[i].selectorText);
if(document.getElementById("idname").hasAttribute("style")){
  alert("Style attribute found");
}else{
  alert("Style attribute not found");
}