【问题标题】:Why there is an extra space on the button in Javascript?为什么Javascript中的按钮上有一个额外的空间?
【发布时间】:2015-08-12 03:25:18
【问题描述】:

我一直在努力解决这个问题,但经过 4 个小时的挖掘和盯着我的代码后,我放弃了它!

代码:

<!DOCTYPE html>
<html>
<head>
<title>Listening to DOM events</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html,body,#map-canvas {
    height: 100%;
    width: 100%;    
    margin: 0;
    padding: 0;
}

.controls {
    margin-top: 0px;
    border: 1px solid transparent;
    border-radius: 2px 0 0 2px;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    height: 32px;
    outline: none;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

#pac-input {
    background-color: #fff;
    font-family: Roboto;
    font-size: 15px;
    font-weight: 300;
    margin-left: 5px;
    padding: 0 11px 0 13px;
    text-overflow: ellipsis;
    width: 240px;
}

#pac-input:focus {
    border-color: #4d90fe;
}

.pac-container {
    font-family: Roboto;
}

#type-selector {
    color: #fff;
    background-color: #4d90fe;
    padding: 5px 11px 0px 11px;
}

#type-selector label {
    font-family: Roboto;
    font-size: 13px;
    font-weight: 300;
}
</style>

<title>Places search box</title>


<script 

    src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>

<script>

var map;
var marker;
var latLng;

function initialize() {

      var myLatlng = new google.maps.LatLng(xxxx, -xxxxx);
      var mapOptions = {
      zoom: 10,
      mapTypeControl: false,
      navigationControl: false,
      scrollwheel: false,
      scaleControl: true,
      center: myLatlng
  };

map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

}  //End of OnStart


google.maps.event.addDomListener(window, 'load', initialize);


</script>

    <style>
      #panel {
        position: absolute;
        top: 0px;
        left: 50%;
        margin-left: -180px;
        width: 240px;
        z-index: 5;
        background-color: #00ffec;
        padding: 5px;
        border: 1px solid #999;
      }
      #latlng {
        width: 240px;
      }

      #target {
        width: 240px;
      }

    </style>

  </head>
  <body>
    <div> 
      <input id="pac-input" class="controls" type="text" placeholder=""> 
      <input type="button" value="Clear" style="Float:right" style="width:60px; height:40px"
                                                                                onclick="Clear_SearchText()">    
    </div>  
    <div id="map-canvas"></div>
  </body>
</html>

谁能帮我理解为什么有一个额外的空间占据了我的布局空间????

非常感谢

编辑:重新上传更好的图片。

【问题讨论】:

  • 它是由包含文本字段和按钮的 div ......
  • 是的,也许问题不清楚,但我不明白其中的困惑。输入文本字段正在创建垂直空间,并且按钮设置为向右浮动,从而为您提供水平空间。不神秘?
  • 我用画图软件删除了文本框(位于那个多余空间的底部。”多余的空间属于这个按钮。我编辑了我的主题。谢谢
  • 您发布了一个问题并手动编辑了您的屏幕截图以隐藏问题的根源?哎呀...

标签: javascript button imagebutton


【解决方案1】:

文本input 的容器div 和按钮input 将占据该位置。 最简单的方法是设置按钮 inputdiv 高度匹配。使按钮匹配的示例:

为按钮添加 id 以引用它:

<input id="clearbutton" type="button" value="Clear" style="Float:right" style="width:60px; height:40px" onclick="Clear_SearchText()">

在css中设置高度:

<style>
#clearbutton {
    height: 32px;
}
</style>

示例:http://jsfiddle.net/ddan/pqdtgmdp/1/

编辑

因此,根据您的完整示例:

<!DOCTYPE html>
<html>
<head>
<title>Listening to DOM events</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html,body,#map-canvas {
    height: 100%;
    width: 100%;    
    margin: 0;
    padding: 0;
}

.controls {
    margin-top: 0px;
    border: 1px solid transparent;
    border-radius: 2px 0 0 2px;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    height: 32px;
    outline: none;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

#pac-input {
    background-color: #fff;
    font-family: Roboto;
    font-size: 15px;
    font-weight: 300;
    margin-left: 5px;
    padding: 0 11px 0 13px;
    text-overflow: ellipsis;
    width: 240px;
}

#pac-input:focus {
    border-color: #4d90fe;
}

.pac-container {
    font-family: Roboto;
}

#type-selector {
    color: #fff;
    background-color: #4d90fe;
    padding: 5px 11px 0px 11px;
}

#type-selector label {
    font-family: Roboto;
    font-size: 13px;
    font-weight: 300;
}
</style>

<title>Places search box</title>


<script 

    src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>

<script>

var map;
var marker;
var latLng;

function initialize() {

      var myLatlng = new google.maps.LatLng(xxxx, -xxxxx);
      var mapOptions = {
      zoom: 10,
      mapTypeControl: false,
      navigationControl: false,
      scrollwheel: false,
      scaleControl: true,
      center: myLatlng
  };

map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

}  //End of OnStart


google.maps.event.addDomListener(window, 'load', initialize);


</script>

    <style>
      #panel {
        position: absolute;
        top: 0px;
        left: 50%;
        margin-left: -180px;
        width: 240px;
        z-index: 5;
        background-color: #00ffec;
        padding: 5px;
        border: 1px solid #999;
      }
      #latlng {
        width: 240px;
      }

      #target {
        width: 240px;
      }

      #clearbutton, #pac-input{
        height: 32px;
      }
    </style>
  </head>
  <body>
    <div> 
      <input id="pac-input" class="controls" type="text" placeholder=""> 
      <input id="clearbutton" type="button" value="Clear" style="Float:right" style="width:60px; height:40px"
                                                                                onclick="Clear_SearchText()">    
    </div>  
    <div id="map-canvas"></div>
  </body>
</html>

【讨论】:

  • 谢谢你的回答,但我几天前开始学习 Javascript 时不知道如何使用 css。我正在使用 Eclipse 编写 Javascript 代码。我正在使用这个 Javascript 为我的个人 Android 应用程序创建地图。你能告诉我如何把它放在 Javascript 中吗?也许,你能告诉我如何使用css吗?非常感谢您的帮助。
  • 我怀疑你是这些东西的新手,这就是为什么我把css代码放在&lt;style&gt;标签中。您只需将其复制到您的 HTML 代码中即可使用
  • 对不起,我今天上班睡觉了。我已经尝试过你上面提到的代码,但它仍然和以前一样。奇怪的是,打开应用程序时看起来还不错(没有多余的空间),但是在加载完所有内容后,多余的空间会出现并向下推搜索框。
  • 我忘了说如果我删除了 clearButton,这个额外的空白就完全消失了。每当添加此 clearButton 时,不知何故会有一个隐藏的布局/面板...
  • 问题是文本输入和按钮在同一个div元素中,如果它们的高度不一样,你会得到一个空白来填补这个空白。我将再次编辑代码以将两个高度设置为相同的值。
猜你喜欢
  • 1970-01-01
  • 2023-02-26
  • 2014-06-22
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 2017-12-13
相关资源
最近更新 更多