Javascript 如何检查html元素是否在IE6-IE8的样式标记中设置了一些样式属性?
Javascript 如何检查html元素是否在IE6-IE8的样式标记中设置了一些样式属性?,javascript,html,css,Javascript,Html,Css,var bg_img=curr_elem.style.backgroundImage | curr_elem.style.getAttribute(“背景图像”) 如果curr\u elem在标记中设置了此属性,而不是在元素本身中设置为:style=“background image:url(blah.png);“,则返回null 那么,我如何知道代码中的某个背景图像是否应用于该元素 我们可以在IE9+中使用getComputedStyle,但我需要在IE6中使用它+ <html>
var bg_img=curr_elem.style.backgroundImage | curr_elem.style.getAttribute(“背景图像”)代码>
如果curr\u elem
在
标记中设置了此属性,而不是在元素本身中设置为:style=“background image:url(blah.png);“
,则返回null
那么,我如何知道代码中的某个背景图像是否应用于该元素
我们可以在IE9+中使用getComputedStyle,但我需要在IE6中使用它+
<html>
<head>
<title>test clone elements</title>
<style type ="text/css">
#banner-map-225 .banner-map
{
background-image: url(test_bg.jpg);
}
</style>
</head>
<body>
<div id = "banner-map-225" ></div>
<div id = "inline_styled_div" style = "background-image: url(test_bg.jpg);" ></div>
<input type = "button" value = "test first div" onclick = "alert(document.getElementById('banner-map-225').style.backgroundImage);" />
<input type = "button" value = "test second div" onclick = "alert(document.getElementById('inline_styled_div').style.backgroundImage);" />
</body>
</html>
测试克隆元素
#横幅地图-225。横幅地图
{
背景图片:url(test_bg.jpg);
}
有点古怪,但它应该能工作
var theDiv = document.getElementById('banner-map-225');
var styleArr = document.getElementsByTagName('STYLE');
var startPos;
var endPos;
var textFromStyle;
var theImageText;
for (var i = 0; i < styleArr.length; i++) {
// find the element selector
if (styleArr[i].innerHTML.toUpperCase().indexOf(theDiv.id) === -1) {
// use class name
startPos = styleArr[i].innerHTML.toUpperCase().indexOf(theDiv.className.toUpperCase());
if (startPos > -1) {
startPos = startPos + theDiv.className.length;
}
} else {
// use id
startPos = styleArr[i].innerHTML.toUpperCase().indexOf(theDiv.id.toUpperCase());
if (startPos > -1) {
startPos = startPos + theDiv.id.length;
}
}
if (startPos > -1) {
// get everything after selector
textFromStyle = styleArr[i].innerHTML.substring(startPos);
// find the style def for the element
startPos = textFromStyle.indexOf('{');
endPos = textFromStyle.indexOf('}');
if (startPos > -1) {
textFromStyle = textFromStyle.substring(startPos, endPos + 1);
// find bg image def
startPos = textFromStyle.toUpperCase().indexOf('BACKGROUND-IMAGE');
endPos = textFromStyle.indexOf(')');
if (startPos > -1) {
// get contents of url
startPos = startPos + ('BACKGROUND-IMAGE').length;
textFromStyle = textFromStyle.substring(startPos, endPos + 1);
startPos = textFromStyle.toUpperCase().indexOf('URL(') + ('URL(').length;
endPos = textFromStyle.indexOf(')');
if (startPos > -1) {
theImageText = trimString(textFromStyle.substring(startPos, endPos));
break;
}
}
}
}
}
// here's your image
if (theImageText) {
alert(theImageText);
}
// no trim in early IE
function trimString(sText) {
return sText.replace(/^\s+|\s+$/g, '');
}
var theDiv=document.getElementById('banner-map-225');
var styleArr=document.getElementsByTagName('STYLE');
var startPos;
var endPos;
var-textFromStyle;
图像文本;
for(var i=0;i-1){
startPos=startPos+theDiv.className.length;
}
}否则{
//使用id
startPos=styleArr[i].innerHTML.toUpperCase().indexOf(theDiv.id.toUpperCase());
如果(startPos>-1){
startPos=startPos+第四id.长度;
}
}
如果(startPos>-1){
//在选择器之后获取所有内容
textFromStyle=styleArr[i].innerHTML.substring(startPos);
//查找元素的样式定义
startPos=textFromStyle.indexOf('{');
endPos=textFromStyle.indexOf('}');
如果(startPos>-1){
textFromStyle=textFromStyle.substring(startPos,endPos+1);
//查找bg图像定义
startPos=textFromStyle.toUpperCase().indexOf('BACKGROUND-IMAGE');
endPos=textFromStyle.indexOf(');
如果(startPos>-1){
//获取url的内容
startPos=startPos+('BACKGROUND-IMAGE')。长度;
textFromStyle=textFromStyle.substring(startPos,endPos+1);
startPos=textFromStyle.toUpperCase().indexOf('URL(')+('URL(')).length;
endPos=textFromStyle.indexOf(');
如果(startPos>-1){
theImageText=trimString(textFromStyle.substring(startPos,endPos));
打破
}
}
}
}
}
//这是你的照片
如果(图像文本){
警报(图像文本);
}
//早期不修剪
函数字符串(sText){
返回sText.replace(/^\s+|\s+$/g');
}
@Hearner-出于某种原因,currentStyle.backgroundImage被设置为“无”,虽然这个元素实际上有一个背景图像,它是在
标记中设置的。我正试图为您找到一个解决方案。在这里的前面,您可以解析样式
标记的内部HTML,并从element@WhiteHat-好的,等一下,我将撰写一个工作示例。这是。…非常…奇怪的解决方案,但在没有找到其他解决方案的情况下,它确实可以工作。谢谢。发现bg图像部分存在一个小问题,请检查编辑。如果我们讨论的是IE6,则没有其他方法。