为什么我的JQuery DIV透明?

时间:2012-06-20 06:56:02

标签: javascript jquery html css

我的Jquery ajax页面加载并显示在DIV中,它放在其他页面元素上,但DIV是透明的,或者它实际上是在其他组件后面,看起来它是透明的并且需要在顶部渲染。如何在白色背景的其他页面上实现正常的DIV?为什么难以使元素不透明?对于页面元素,这怎么会是一种愚蠢的行为呢?

enter image description here

的Javascript

function popup() {
    alert('opening popup');
    var popup = $('.newpopup');

  $.ajax({url:'/PandoraArendeWeb/popup.jsp',
            error: function() {
                alert('Error');
            },
            success: function(data) {   
                popup.html(data); 
                popup.show('fast');
            }
       }
   ); 

   /*
    popup.html("test"); 
    popup.show('fast');
    */
    var screen_width = $(document).width();
var screen_height = $(document).height();
var box_width = popup.width();
var box_height = popup.height();

var top = (screen_height - box_height) / 2; // you might like to subtract a little to position it slightly higher than half way
var left = (screen_width - box_width) / 2;
    popup.css({ 'position': 'absolute', 'top':top, 'left':left });
}
$(document).ready(function(){
  $('button').click(function(){
     popup();
  });
})

我尝试创建一个jsfiddle来重现问题,但似乎jsfiddle无法执行.get.ajax,因为它无法将网页从互联网加载到DIV。

你能提出什么建议?应该有一个明确的解决方案,并且透明度实际上不应该是元素的默认状态,那么元素如何以透明度呈现呢?

HTML很简单

<div class="newpopup">
</div>
<button id="mypopup">popup</button>

我的CSS就像从未到过的那样是

.newpopup {
z-index:100;
 position: absolute; 
  top:50%;
  left:50%;
  background-color:#ffffff; //not working
} 

实际的popup / div的HTML就是这个

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head><link href="css_js/styles.css" rel="stylesheet" type="text/css">
<title>popup</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>

<table width="100%" border="0" cellspacing="0" cellpadding="2" align="center" class="TB_nb">
  <tr>
  <td colspan="3" class="pusher TB_nb"><h2>Sök person/företag</h2> 
</td>
  <td><a href="javascript:void(0)" onclick="document.getElementById('popupSokNamn').style.display = 'none';" >X</a></td>
</tr>
</table>
<br><br>
<h2 class="pusher">Sök person/företag</h2>
<div id="Vsok">

<div style="text-align: right; width: 100%; padding-right: 5%; padding-top: 5px;">
<span onClick="getElementById('sokF').style.display='', getElementById('bottomA').style.display='none', getElementById('bottomV').style.display='', getElementById('Vsok').style.display='none'" class="link_sm">Visa s&ouml;kformul&auml;r</span>
</div>

</div>

<div id="sokF">

<div style="text-align: right; width: 100%; padding-right: 5%; padding-top: 5px;; padding-bottom: 5px;">
<span onClick="getElementById('sokF').style.display='none', getElementById('bottomA').style.display='none', getElementById('bottomV').style.display='', getElementById('Vsok').style.display=''" class="link_sm">D&ouml;lj s&ouml;kformul&auml;r</span>
</div>

<div style="width: 100%; margin-left: 15px; margin-right: 80px;" class="fontS80">
<fieldset style="border: 1px solid Grey; display:inline;"><legend class="small">Fysisk</legend> 
<div class="fl30">&nbsp;F&ouml;rnamn:</div>
<div class="fl50"><input type="text" size="60" name="searchFornamn" onkeyup="doSubmitByEnter('Namnsokning', 'search')"></div>
<div class="clear"></div>
<div class="fl30">&nbsp;Efternamn:</div>
<div class="fl50"><input type="text" size="60" name="searchEfternamn" onkeyup="doSubmitByEnter('Namnsokning', 'search')"></div>
</fieldset>

<fieldset style="border: 1px solid Grey; display:inline;"><legend class="small">Juridisk</legend> 
<div class="fl30">&nbsp;F&ouml;retag:</div>
<div class="fl50"><input type="text" size="60" name="searchForetag" onkeyup="doSubmitByEnter('Namnsokning', 'search')"></div>
<div class="clear"></div>
<div class="fl30">&nbsp;Organisationsnummer:</div>
<div class="fl50"><input type="text" size="60" name="searchOrgNummer" onkeyup="doSubmitByEnter('Namnsokning', 'search')"></div>
</fieldset> <br><br>

<!-- <div class="fl30">Attention, c/o etc.:</div>
<div class="fl50"><input type="text" size="60"></div>
<div class="clear"></div>

<div class="fl30">Postadress:</div>
<div class="fl50"><input type="text" size="60"></div>
<div class="clear"></div>

<div class="fl30">Postnummer:</div>
<div class="fl50"><input type="text" size="30"></div>
<div class="clear"></div> -->

<div class="fl30">Postort:</div>
<div class="fl50"><input type="text" size="40" name="searchPostort" onkeyup="doSubmitByEnter('Namnsokning', 'search')"></div>
<div class="clear"></div>

<div class="fl30">Land:</div>
<div class="fl50"><input type="text" size="2" name="searchLandKod" onkeyup="doSubmitByEnter('Namnsokning', 'search')">&nbsp;
                    <select name="searchLand" onkeyup="doSubmitByEnter('Namnsokning', 'search')">
                        <option value="1" SELECTED></option>
                        <option value="2"></option>
                        <option value="3"></option>
                        <option value="4"></option>
                        <option value="5">---------------------------------</option>
</select></div>
<div class="clear"></div>

<!-- <div class="fl30">Region:</div>
<div class="fl20"><select name="">
                        <option value="1" SELECTED></option>
                        <option value="2"></option>
                        <option value="3"></option>
                        <option value="4"></option>
                        <option value="5">-----------------------------------------------</option>
</select></div>
<div class="clear"></div>
<div class="fl30">Tel:</div>
<div class="fl50"><input type="text" size="40"></div>
<div class="clear"></div>

<div class="fl30">Fax:</div>
<div class="fl50"><input type="text" size="40"></div>
<div class="clear"></div>

<div class="fl30">E-post:</div>
<div class="fl50"><input type="text" size="60"></div>
<div class="clear"></div>
 -->
<div class="fl50">&nbsp;</div>
<div class="fl5"><input type="button" value="Rensa"></div>
<div class="fl10"><input type="button" value="  S&ouml;k  " onclick="javascript:doSubmit('Namnsokning', 'search')"></div>
<div class="clear">&nbsp;</div>
<div class="clear">&nbsp;</div>
</div>

</div>

<table width="100%" border="0" cellspacing="0" cellpadding="4" align="center">
<tr>    
    <td><h3>Sökresultat:</h3></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
<tr>    
    <td colspan="4">En massa text <span class="link">Hj&auml;lp!</span> </td>
</tr>
<tr>    
    <td><input type="button" value="Visa alla"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
<tr class="smallb">
    <td>Antal &auml;renden: 527</td>
    <td>&nbsp;</td>
    <td>Visa &auml;renden: &#60;&#60; 1-200 201-400 401-527 &#62;&#62; </td>
<td>&nbsp;</td>
</tr>
</table>

<table width="100%" cellspacing="0" align="center" class="sortable" id="unique_id">
<tr>
    <th class="thkant">F&ouml;rnamn</th>
    <th class="thkant">Efternamn</th>
    <th class="thkant">Adress</th>
    <th class="thkant">Postnr</th>
    <th class="thkant">Postort</th>
    <th class="thkant">Region</th>
    <th class="thkant">Land</th>
    <th class="thkant">Telefonnummer</th>
</tr>


</table>


<div id="bottomV">
<table width="100%" align="center">
<tr>
<td align="left"><input type="button" id="visaknapp" value="Visa" disabled style="width:150px;" onClick="getElementById('sokR').style.display='', getElementById('bottomA').style.display='', getElementById('bottomV').style.display='none', getElementById('Vsok').style.display='', getElementById('sokF').style.display='none'"></td>
<td align="right"><input type="button" value="Avbryt" style="width:150px;" class="checkmargin"><input type="button" value="Infoga" disabled style="width:150px;"></td>
</tr>
</table>
</div>

<div id="bottomA" style="display: none">
<table width="100%" align="center">
<tr>
<td align="left"><input type="button" value="&Auml;ndra i register" style="width:150px;"></td>
<td align="right"><input type="button" value="Avbryt" style="width:150px;" class="checkmargin"><input type="button" value="Infoga" style="width:150px;"></td>
</tr>
</table>

</body>
</html>

4 个答案:

答案 0 :(得分:2)

  

应该有一个明确的解决方案和透明度   不应该是元素的默认状态,所以元素是怎么来的   渲染透明度?

实际上,除非您为元素指定显式背景颜色,否则默认 它们是透明的。这样可以显示真实的页面背景。

只需在弹出式div 上添加背景颜色(以及适当的z-Index)然后使其显示为预期的叠加层。

如果它不起作用,则某些会阻止您提供的(正确的)CSS使用。

也许您应该从加载的页面中删除额外的HTML正文等?您不应该同时在页面中显示两个<body>标记。如果您无法更改已加载的页面(例如,如果它也用作独立页面),请尝试以下操作:

popup.empty().append($('body', data).children());

而不是:

popup.html(data);

答案 1 :(得分:2)

使用以下代码将背景颜色应用于div

$(“。newpopup”)。css('background-color','white');

答案 2 :(得分:1)

所以,如果我正在读这个,你的jQuery脚本正在读一个.jsp文件,它有完整的标题标题,正文等......

您应该将弹出窗口的内容放入iFrame或删除<body>之前(包括)</body>之后的所有标记

答案 3 :(得分:0)

您将在.newpopup类

中尝试此属性
'overlayShow'   : true,
'transitionIn'  : 'elastic',
'transitionOut' : 'elastic',
'opacity'       : '0.80';

#popup {
margin: 0 auto;
height:100%;
width: 100%;
z-index:501;
background: #effeef;
position: absolute;
padding:0;
top: 0;
-moz-opacity:0.80;
-khtml-opacity: 0.80;

opacity: 0.80;
filter:alpha(opacity=100);
}

#window {
padding:0;
width: 200px;
height: 200px;
margin: 0 auto;
border: 3px solid #d3d4d3;
background:url(sign_in_bg.gif) repeat-x;
position: absolute;
top: 200px;
left: 45%;
}

<div id="popup" align="center" style="display:none">
<div id="window" align="center">
content....
</div>