javascript - 当输入宽度在 Safari 中变小时,表单不会缩小

标签 javascript html css safari

我用 javascript 编写了这个小代码:

function clicksearchicon(searchbar) {
var searchbar = document.getElementById(searchbar),
searchbar_class = searchbar.className;
if(searchbar_class == "searchbar_hidden"){
    searchbar.className = "searchbar";
    return false;
}
else{
    if(searchbar.value == '' || searchbar.value == searchbar.defaultValue){
        searchbar.className = "searchbar_hidden";
        return false;
    }
    else{
        return true;
    }
}
}

html:

<form action="search.php" method="get" onsubmit="return clicksearchicon('searchbar')">
<input type="text" name="q" placeholder="Search..." class="searchbar_hidden" id="searchbar" autocomplete="off">
<input type="submit">
</form>

和CSS:

.searchbar_hidden {
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-o-transition: all 1s ease;
-ms-transition: all 1s ease;
transition: all 1s ease;
width: 0px !important;
padding: 0px !important;
margin: 0px !important;
opacity: 0;
}

.searchbar {
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-o-transition: all 1s ease;
-ms-transition: all 1s ease;
transition: all 1s ease;
width: 130px !important;
opacity: 1;
}

在 Chrome、Firefox、IE 和 Opera 中一切正常,但在 Safari 中,当输入字段隐藏时,表单不会缩小,提交按钮将向左浮动,右侧有较大的空白。谁能帮帮我?

最佳答案

编辑:

好的,看来是被ID值改变了。我已经用一个快速而肮脏的解决方案修复了它。非常直截了当,只是为了向您展示行为。对我有用:见fiddle .

HTML:

<form action="search.php" method="post" class="searchbar_hidden" onsubmit="return clicksearchicon('searchbar')" width="1" >
    <input type="text" name="q" placeholder="" class="searchbar_hide" id="searchbar" autocomplete="off" />
    <input type="Submit" name="nameSubmit" id="idButton_Submit" value="Submit"  alt="Submit Button"/>
</form>

JavaScript:

clicksearchicon = function(searchbar){
    var searchbar = document.getElementById(searchbar);
    if(searchbar == null){
        var searchbar = document.getElementById('searchbar_show');
    }
    searchbar_class = searchbar.className;
    if(searchbar_class == "searchbar_hide"){
        searchbar.className = "searchbar_show";
        searchbar.id = "searchbar_show";
        return false;
    }
    else{
        if(searchbar.value == '' || searchbar.value == searchbar.defaultValue){
        searchbar.className = "searchbar_hide";
        searchbar.id = "searchbar";
        return false;
    }
    else{
        return true;
    }
}
}

CSS:

#searchbar {
    width:1px;
}

#searchbar_show {
    width:130px;
}


.searchbar_hidden .searchbar_hide {
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-o-transition: all 1s ease;
-ms-transition: all 1s ease;
transition: all 1s ease;
width: 0x !important;
padding: 0px;
margin: 0px;
border:none;
opacity: 0;
}

.searchbar_hidden .searchbar_show {
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-o-transition: all 1s ease;
-ms-transition: all 1s ease;
transition: all 1s ease;
width: 130px;
}

希望这有助于...

关于javascript - 当输入宽度在 Safari 中变小时,表单不会缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728553/

相关文章:

javascript - 为什么 Handlebars.js 第一次绑定(bind)后要花这么长时间?

javascript - 如何将 jQuery live() 函数应用于此代码?

javascript - 单击并拖动时如何避免将焦点集中在 div 上?

javascript - 选择日期后如何删除 html 5 中的 X 标记(清除按钮)[输入类型 ="date"]

html - 当我尝试查询其他国家/地区的es-es时,抓取news.google.com将hl和ceid参数添加为en-us

css - 我如何设置 2 张图像相互重叠

javascript - 添加包含下拉列表和动态内容的 div,以及更多 JS

html - 如何在弹出窗口中隐藏特定行

html - 每个图像的单独文本叠加

html - 在 Chrome 中相对定位元素后选择无法访问的元素