这是html代码:
<div id="popup" class="popup_block" >
<img src="images/PUB-histRIRE.jpg" alt="popup" />
</div>
和脚本:
<script type="text/javascript">
$(document).ready(function(){
popWidth = 690;
popHeight = 550;
popID = 'popup';
var popMargTop = popHeight / 2;
var popMargLeft = popWidth / 2;
//Apply Margin to Popup
$('#' + popID).css({
'width': popWidth,
'height': popHeight,
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft,
'visibility' : 'visible'
});
//Fade in Background
$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer
//Close Popups and Fade Layer
$('a.close, #fade, .popup_block').live('click', function() { //When clicking on the close or fade layer...
$('#fade , .popup_block').fadeOut(); //fade them both out
$('#fade').remove();
return false;
});
});
</script>
我喜欢只在有 div 的页面上执行代码...在没有 thi div 的页面上,忽略即可。我可以通过解析 URL 来创建一个 if...但它对我来说看起来更复杂...任何简单的 jquery 技巧?
最佳答案
if($('#popup').length >0 ){
//your code here
}
关于jquery - 仅当 div 存在时才执行 jquery 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5950435/