可调节的量程标签,用于显示onclick事件计数

时间:2012-09-03 00:23:46

标签: javascript html button onclick

我目前有一个span标签,显示按钮的onclick计数。现在如果数字超过三位数,它将不适合span标记。我可以使span标签可调,还是可以调整其宽度,以便它始终包含onclick计数?

我可以更改宽度以容纳更多数字,但我希望它可以调整,因为我无法预测实际会发生多少次onclick事件。

var counts = {
  'Apples': 0,
  'Oranges': 0,
  'total': 0
};

   function countFruit(type) {
   document.getElementById('fruitCount').innerHTML = ++counts['total'];
   document.getElementById(type + 'Count').innerHTML = ++counts[type];
}


#OrangesCount{
z-index: 20;
position:absolute;
top:70px;
left:45%;
background-color:black;
width:80px;
border:2px solid green;
font-family: 'BPdotsUnicaseSquareBold';
font-size:25px;
color:yellow;
padding-right:4px;
padding-left:4px;
 }


#ApplesCount{
z-index: 20;
position:absolute;
color:yellow;
top:70px;
right:45%;
background-color:black;
width:80px;
border:2px solid green;
font-family: 'BPdotsUnicaseSquareBold';
font-size:25px;
   }

   <a id="buttonright" href="#" onclick="countFruit('Apples'); return false;">
   <a id="buttonleft" href="#" onclick="countFruit('Oranges'); return false;">

   <span id="ApplesCount"></span>
   <span id="OrangesCount"></span>

2 个答案:

答案 0 :(得分:0)

您只需要从两个css中删除width

#ApplesCount{
    z-index: 20;
    position:absolute;
    color:yellow;
    top:70px;
    right:45%;
    background-color:black;
    border:2px solid #550010;
    font-family: 'BPdotsUnicaseSquareBold';
    font-size:25px;
}

这是JSFiddle

答案 1 :(得分:0)

如果您需要允许元素自动调整其宽度,请不要将其宽度固定。您可以指定最小宽度,如下面的示例CSS。

#OrangesCount{
z-index: 20;
position:absolute;
top:70px;
left:45%;
background-color:black;

/*removed
width:80px;
*/
min-width:80px;

border:2px solid #001855;
font-family: 'BPdotsUnicaseSquareBold';
font-size:25px;
color:yellow;
padding-right:4px;
padding-left:4px;
}


#ApplesCount{
z-index: 20;
position:absolute;
color:yellow;
top:70px;
right:45%;
background-color:black;

/*removed
width:80px;
*/
min-width:80px;

border:2px solid #550010;
font-family: 'BPdotsUnicaseSquareBold';
font-size:25px;
}
相关问题