【问题标题】:jQuery problem in IEIE中的jQuery问题
【发布时间】:2011-07-04 11:56:30
【问题描述】:

我的简单 jQuery 脚本在 IE 中不起作用。

请帮忙。

完整的 HTML 代码在这里:

<script type="text/javascript" src="jquery-1.4.2.min.js" ></script>
<script type="text/javascript">
$(document).ready(function() {
        var cvalue = $(".space").css("backgroundColor");
        $(".space").css("color",cvalue);    
   });
</script>

<style>
table.tdata td {
    padding: 10px;
}
tr.zrow1 {
    background-color: #ccc
}
tr.zrow2 {
    background-color: #aaa
}
tr.zrow1 td, tr.zrow2 td {
    border-top: 1px solid white;
    border-left: 1px solid #ccc;
}
table.tdata tr.thead1 {
    text-align: center;
}

table.tdata tr.thead {
    background-color: #003;
    color: white;
    text-align: center;
}
.thrColFixHdr #header h1 {
    margin: 0; 
    padding: 10px 0; 
}
.thrColFixHdr #mainContent {
    margin: 0 200px; 
    padding: 0 10px; 
}
</style>

<div id="areaRec">
<h2 style="margin-top: 0pt;">Sample text</h2>
<p>Sample text  Sample text Sample text Sample text Sample text Sample text Sample text Sample text<span class="space">_</span>text Sample text Sample text Sample text Sample text<br />text Sample text Sample text Sample text Sample text text Sample text Sample text Sample text Sample text text Sample text Sample text Sample text Sample text text Sample text Sample text Sample text Sample text text Sample text Sample text Sample text Sample text</p>
<h3 style="margin-top: 0pt;">text Sample text Sample text </h3>
<p>Sample textSample textSample textSample textSample textSample text<span class="space">_</span>Sample textSample textSample textSample textSample text<span class="space">_</span>Sample textSample textSample text  textSample textSample textSample textSample textSample textSample textSample textSample textSample textSample textSample textSample textSample textSample text.</p>
<h3 style="margin-top: 0pt;">Sample textSample textSample textSample text</h3>
<div class="show_job">
<table class="job-position" border="0" cellspacing="1" cellpadding="4">
<tbody>
<tr class="zrow2">
<td class="title-col" width="181" valign="top">Sample text</td>
<td width="457" valign="top">
<ul>
<li>text Sample text Sample text Sample text Sample text<span class="space">_</span>Sample text</li>
<li>text Sample text Sample text Sample text Sample text<span class="space">_</span>Sample text</li>
<li>text Sample text Sample text Sample text Sample text<span class="space">_</span>Sample text Sample text<span class="space">_</span>Sample text:</li>
</ul>
<blockquote><ol>
<li>Adobe Photoshop</li>
<li>Adobe InDesign</li>
<li>Adobe Illustrator</li>
</ol><ol> </ol></blockquote>
<blockquote><ol> </ol></blockquote>
</td>
</tr>
<tr class="zrow1">
<td class="title-col" width="181" valign="top">Sample text</td>
<td width="457" valign="top">Sample text</td>
</tr>
<tr class="zrow2">
<td class="title-col" width="181" valign="top">Sample text</td>
<td width="457" valign="top">Sample text</td>
</tr>
<tr class="zrow1">
<td class="title-col" width="181" valign="top">Sample text</td>
<td width="457" valign="top">Sample text</td>
</tr>
<tr class="zrow2">
<td class="title-col" width="181" valign="top">Sample text </td>
<td width="457" valign="top">Sample text</td>
</tr>
<tr class="zrow1">
<td class="title-col" width="181" valign="top">Sample text </td>
<td width="457" valign="top">Sample text<span class="space">_</span>Sample textSample textSample textSample text<span class="space">_</span>Sample textSample textSample textSample textSample text </td>
</tr>
</tbody>
</table>
</div>
</div>

我认为 IE 不理解 css 中的透明值,有什么解决办法吗?

最终解决方案:

$('.space').each(function(index, value){
var cvalue = $(".space").css("backgroundColor");
if(cvalue == '' || cvalue == 'transparent'){
 var pcolor = ($(this).closest('div, p, tr').css('backgroundColor'));
 if (pcolor == '' || pcolor == 'transparent'){
  //this means the closet tag hasnt any bgcolor and it is white 
      $(this).css('color','white');
 }else {
  $(this).css('color',pcolor);
 }
}else{
 $(this).css('color',cvalue);
}
});

【问题讨论】:

  • 你能不能也给我们看看你的相关html?我的意思是关于 .space
  • 那为什么在ff、safari、opera和chrome等所有标准浏览器中都可以?

标签: jquery internet-explorer coding-style


【解决方案1】:

您的代码末尾缺少一个分号 添加它应该没问题 - http://jsbin.com/anuric/2/

$(document).ready(function() { var cvalue = $(".space").css("background-color"); alert(cvalue); $(".space").css("color",cvalue); });

更新;

<script type="text/javascript">
    $(document).ready(function() {
        var cvalue = $(".space").css("background-color");
        alert(cvalue);
        $(".space").css("color",cvalue);    
    });
</script>

使用您的 HTML 更新; http://jsbin.com/asigah/

Update2 - 确保您的 CSS 中有 .space background-color 的 CSS 值,如果 .space 不存在,则 cvalue 无法找到 .space 的背景色。

.space { background-color: #fff; }

更新 3 - 如果您只是想“隐藏” .space 的内容并在那里只有一个空白空间,请使用它;

$(document).ready(function() {
    $('.space').replaceWith('&nbsp;'); 
});

这里的工作示例; http://jsbin.com/asigah/3

更新 4;

$(document).ready(function() {
  $('.space').each(function(index, value){
    var cvalue = ($(this).closest('div, p, tr').css('background-color'));
    //alert(cvalue);
    $(this).css('color',cvalue);
  });
});

这将找到最接近的元素(div、p 或 tr)标签,并将.space 的颜色设置为与其找到的元素的背景颜色相同。 我想这就是你想要的。

在此处预览 - http://jsbin.com/asigah/28

【讨论】:

  • 我照你说的做了,但没有改变!
  • 你需要在 CSS 中为 .space 指定背景颜色,如果不存在则无法选择。我已经更新了答案
  • 问题就在这里!! .space 没有特定的背景颜色。我只想使用继承功能。我的意思是,如果不是他的风格得到他的父风格。
  • 如果不指定背景色,它会将背景色拾取为透明,这仍然有效吗?
  • 我测试了 background-color: transparent 再次没有结果。
【解决方案2】:

如果您将背景颜色添加到 .space,此代码将起作用使用 css p>

$(document).ready(function() {
     var cvalue = $(".space").css("backgroundColor");
     $(".space").css("color",cvalue);    
});

注意:

代码有效。但它为.space 添加了transparent 颜色,因为.spacecss 中没有定义背景。所以你需要将backgound颜色添加到.space

此代码适用于您当前的 HTML 和 CSS 结构,没有任何变化:

$(document).ready(function() {
    $('.space').each(function(index, value){
      var cvalue = $(this).parents().find('tr').css('backgroundColor');
      $(this).css('color', cvalue);
    });
});

我又添加了一个代码 sn-p:

$(document).ready(function() {
    $('.space').each(function(index, value) {
        var that = this;
        $(this).parents().each(function() {
            var cvalue = $(this).css('backgroundColor');
            if (cvalue != '' && cvalue != 'transparent') {
                $(that).css('color', cvalue);
            }
        });
    });
});

【讨论】:

  • 我真的不知道发生了什么,它在我的 IE 中根本不起作用。
  • @Matt Ston 我在 .space 的 css 中没有看到任何背景颜色。你想添加什么颜色。背景grey颜色或其他东西
  • 感谢亲爱的 abdullah,但我无法为 .space 设置特定样式:(
  • 如果你不想给.space设置任​​何特定的样式,那么使用第二个代码sn-p。在第二个代码中,它从其父级tr 获取background-color,您将background-color 分配给它,并将该颜色分配给.space。我想你想要这个。
  • 这个脚本会添加到我所有的页面中,我在所有页面中都没有相同的 tr,这只是其中之一,这种情况的解决方案是什么?我真的很困惑,因为这个脚本在 ff 中可以正常工作,只有 IE socks :((
猜你喜欢
  • 2011-06-25
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多