对于基于Blogger的my site,我使用名为“最近的文章”的小部件。这些样式适用于Firefox和Chrome,但不适用于IE。
我是网络开发的新手,不懂JS。
.bp_item_title {
margin: 10px 0;
padding: 0;
font-family: Tahoma,Geneva,sans-serif;
font-size: 14px;
}
我在IE上的firebug中对此进行了测试,看起来CSS样式没有附加到元素上。为什么IE不会将这些CSS样式传递给元素/小部件?
<!-- ... -->
<div class="widget-content">
<div id="bp_recent"><div class="bp_item_title"><a href="http://blog.onlinewagerreview.com/2012/05/miami-heat-vs-boston-celtics-game-2.html?utm_source=BP_recent&utm-medium=gadget&utm_campaign=bp_recent" target="_top" title="Miami Heat vs Boston Celtics, Game 2 Free Pick, Prediction">Miami Heat vs Boston Celtics, Game 2 Free Pick, Prediction</a></div>
if (showThumbs == true && thumbUrl != "") {
myImage = document.createElement('img');
myImage.setAttribute("src", thumbUrl);
if(imgFloat!="none")
{
float_clear=true;
myImage.style.cssFloat=imgFloat;
myImage.style.styleFloat=imgFloat;
}
try{if(myMargin!=0)myImage.style.margin = myMargin+"px";} catch(error){}
myImage.setAttribute("alt", postTitleOriginal);
myImage.setAttribute("width", imgDim);
myImage.setAttribute("height", imgDim);
myLink = document.createElement('a');
myLink.setAttribute("href", postUrl+"?utm_source=bp_recent&utm-medium=gadget&utm_campaign=bp_recent");
myLink.setAttribute("target", "_top");
myLink.setAttribute("title", postTitleOriginal);
myLink.appendChild(myImage);
myDiv = document.createElement('div');
myDiv.setAttribute("class", "bp_item_thumb");
myDiv.appendChild(myLink);
main.appendChild(myDiv);
}
答案 0 :(得分:1)
setAttribute()
在IE中不适用于style
属性。要支持IE,请使用element.className = 'newClass';
。使用检查class
属性是否已设置为非空值并在设置时包含这些值的函数也是一个好主意。例如:
// $el is the element to add $class to
// $class is the string value to append to `class` attribute
function addClass($el, $class) {
if(!$el){
return false;
}
$c = $el.className;
if (($c === null) || ($c === '')) {
$el.className = $class;
} else if ($c.indexOf($class) === -1) {
$el.className = $c + ' ' + $class;
}
}
// $el is the element to remove $class from
// $class is the string value to remove from `class` attribute
function removeClass($el, $class) {
if(!$el){
return false;
}
$c = $el.className;
if (($c !== null) && ($c !== '') && ($c.indexOf($class) !== -1)) {
if ($c.indexOf($class) === 0) $el.className = $c.replace($class, '');
else $el.className = $c.replace(' ' + $class, '');
}
}
或者只是将class
属性设置为新值(而不是附加新类或删除单个类):
myDiv.className = "bp_item_thumb";
......而不是......
myDiv.setAttribute("class", "bp_item_thumb");