按步骤设置div高度以适应平铺背景 [英] Set div height in steps to fit tiled background

查看:100
本文介绍了按步骤设置div高度以适应平铺背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要控制div的高度始终以20为单位出现。我有一个20像素高的重复div背景,我希望div始终以正确的方式结束。我发现我认为是这里的解决方案:
将元素的高度设置为数字的倍数?



但是,当我尝试它不起作用。我是jquery的新手,可能犯了一个简单的错误。

在下面的测试html代码中,我期望div的高度总是以100px的步长出现,但它不起作用。

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://`在此输入代码`www .w3.org / TR / xhtml1 / DTD / xhtml1-transitional.dtd> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< title>测试jquery< / title>
< script type =text / javascriptsrc =jquery.js>< / script>
< script language =javascript>
$(document).ready(function(){
$ b $('#testCol')。height(function(i,h){return Math.ceil(h / 100)* 100;});

});
< / script>

< style type =text / css>
#testCol {
width:150px;
背景颜色:#FFCC00;
}
< / style>
< / head>

< body>

< div id =testCol> df ds d sfdhs fdsf ds fhsdu ds iuhsdi fsdf dhsf sdiuh isdf iusd fiushd fiuds i uhsdiu isudf iusd fiusdh fiusd fih wtww wer rew< / div>

< / body>
< / html>


解决方案

从脚本标记中移除language =javascript 。这是预防浏览器运行脚本。你不需要这个指令,但是如果你想使用它,它应该是:

 < script type =text / JavaScript的> 


I need to control a div's height to always appear in steps of 20. I have a repeating div background that is 20 pixels high and I want the div to always end in a correct way. I found what I think is the solution here: Set height of an element on multiples of a number?

But when I try it doesn't work. I'm a newbie at jquery and probably made a simple mistake.

In the following test html code I'm expecting the div's height to always appear in steps of 100px, but it doesn't work.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://`enter code here`www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test jquery</title>
<script type="text/javascript" src="jquery.js"></script> 
<script language="javascript">
$(document).ready(function(){

    $('#testCol').height(function(i, h) { return Math.ceil(h / 100) * 100; });

});
</script>

<style type="text/css">
    #testCol {
        width: 150px;
        background-color:#FFCC00;
    }
</style>
</head>

<body>

    <div id="testCol">df ds d sfdhs fdsf ds fhsdu ds iuhsdi fsdf dhsf sdiuh isdf iusd fiushd fiuds i uhsdiu isudf iusd fiusdh fiusd fih wtwpr wer rew</div>

</body>
</html>

解决方案

Remove the language="javascript" from the script tag. That's prevening the browser running the script. You don't need this directive but if you want to use it, it should be:

<script type="text/javascript">

这篇关于按步骤设置div高度以适应平铺背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆