js代码无效(span无法设置border)

很多时候写css的时候都会出现一种无力感,别人的代码自己都能看懂,但是自己写的时候效果和自己脑子里的预想有些差距,这里面有各种各样的原因,刚好碰上一个,分享一下,就是没有注意到js语句顺序导致的,js语言里面除了函数的声明会提升,其它的语句都要按顺序执行,如果有语法明显错误报错还好,但如果不报错就可有的找了。
代码:

<!DOCTYPE html>
<html>

    <head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        /*css reset*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: ”;
content: none;
}
/* remember to define focus styles! */
:focus {
outline: 0;
}
/* remember to highlight inserts somehow! */
ins {
text-decoration: none;
}
del {
text-decoration: line-through;
}
/* tables still need ‘cellspacing=”0″‘ in the markup */
table {
border-collapse: collapse;
border-spacing: 0;
}
/*end*/
        #hours{
            line-height:30px ;
        }
        .show{
            box-sizing:border-box;
            height:30px;
            background:#ccc;
            text-align:center;
            border-left: 1px solid red;
        }
    </style>
    </head>
    <body>
        <div id="hours"></div> 
        <div class="pic"></div>
    </body>
    <script>
        hline=document.getElementById("hours");
        hours='';
        hours+='<span class="show" style="width:50px;">小时</span>';
        hours+='<span class="show" style="width:60px">0</span';
        hline.innerHTML=hours;

    </script>

</html>

本意是创建两个元素,中间用边框线分隔,但边框怎么都出不来,搞得我有点怀疑人生,开始怀疑是不是span元素无法设置属性,或者是上下文有些什么复杂的用法,因为很多时候由于自己对css和浏览器样式了解不足会导致一些代码错误,整的十分紧张。搞了半天后发现是因为class属性最先被读取,而width又是在行内样式被定义的,导致border-left: 1px solid red;执行时宽度还没有被定义而导致代码无效,改后代码如下:

<!DOCTYPE html>
<html>

    <head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        /*css reset*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: ”;
content: none;
}
/* remember to define focus styles! */
:focus {
outline: 0;
}
/* remember to highlight inserts somehow! */
ins {
text-decoration: none;
}
del {
text-decoration: line-through;
}
/* tables still need ‘cellspacing=”0″‘ in the markup */
table {
border-collapse: collapse;
border-spacing: 0;
}
/*end*/
        #hours{
            line-height:30px ;
        }
        .show{
            display: inline-block;
            box-sizing:border-box;
            height:30px;
            background:#ccc;
            text-align:center;

        }
    </style>
    </head>
    <body>
        <div id="hours"></div> 
        <div class="pic"></div>
    </body>
    <script>
        hline=document.getElementById("hours");
        hours='';
        hours+='<span class="show" style="width:50px;">小时</span>';
        hours+='<span class="show" style="width:60px;border-left: 1px solid red;">0</span';
        hline.innerHTML=hours;

    </script>

</html>

就可以了,希望这篇文章能为后人节省一点试错的时间,加油!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

returnadsss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值