如何在IE6 - IE8中检查html元素是否在样式标签中设置了一些样式属性
How to check if html element has some style properties set in style tag in IE6 - IE8?
var bg_img = curr_elem.style.backgroundImage || curr_elem.style.getAttribute("background-image");
如果curr_elem
在<style>
标签中设置了此属性,则返回null,而不是像这样:style = "background-image: url(blah.png);"
在元素本身中。
那么,我怎么知道如果一些背景图像应用到这个元素的代码?
我们可以在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>
有点做作,但应该可以…
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, '');
}
相关文章:
- 对插件初始化后动态加载的元素进行样式设置
- 使用jQuery按条件将样式设置为html标记
- 将 href 样式设置为按钮
- 如何以类似于vivus.js的样式设置SVG填充和路径的动画
- 根据用户输入声明样式设置
- 更改:在使用Javascript进行样式设置之前
- 反应:使用内联样式设置所有元素的样式
- 在AngularJS中构建SPA应用程序与纯JavaScript - 选择哪种Boostrap进行样式设置
- 我将如何使用CSS3对动态创建的jQuery UI元素进行样式设置
- Tumblr JS嵌入CSS/HTML标签进行样式设置
- 如何使用 ng 样式设置元素宽度 % 的样式
- 如何使用 Web 浏览器控件进行样式设置,但具有 C# 中的逻辑
- 从 javascript 获取样式设置时出现问题
- 样式设置 jQuery 验证错误消息
- 使用 React-Toolbox 的按钮进行样式设置不起作用
- 如何将离子输入类型文件的样式设置为按钮
- 如何将加载到<对象>中的元素样式设置为数据
- CKEDITOR没有't对来自数据库的文本进行样式设置
- 如何对一个元素进行ng样式设置's$索引由ng repeat创建
- D3可以't将样式设置为带标题的对象