1
を示して、私は次のコードを持っている:を非表示と入力doesntの仕事
\t var getInput = document.querySelector('.search-form')
var screenWidth;
var getWidth;
var bodyDisplay;
var display;
window.onresize = function(event) {
\t screenWidth = event.target.outerWidth;
};
\t \t \t \t \t
function getCssProperty(elmId, property){
\t \t var elem = document.getElementById(elmId);
\t \t return window.getComputedStyle(elem,null).getPropertyValue(property);
}
\t \t \t \t \t \t
function showInput() {
\t if(screenWidth === null){
\t \t screenWidth = document.documentElement.clientWidth;
\t }
\t if(display === 'none' && screenWidth > 959){
\t \t getInput.style.display = 'inline-block';
\t \t document.querySelector('.search-field').focus();
\t } else {
\t \t getInput.style.display = 'none';
\t }
\t \t \t \t \t \t
}
document.getElementsByTagName('body')[0].onclick=function(){
\t display = getCssProperty('search-field', 'display');
\t if(bodyDisplay === 'inline-block') {
\t \t getInput.style.display = 'none';
\t } else {}
};
.search-container {
background: #fff;
}
.search-form {
position: relative;
background: #000;
width: 250px;
display: none;
z-index: 9999;
}
.search-field {
margin: 0;
}
.search-field:hover,
.search-field:focus {
border: 1px solid #0cb4ce;
}
button {
display: block;
}
<button onclick="showInput()">Press here</button>
\t <form action="#" class="search-form" id="search-field" method="get">
\t \t <div class="search-container-inner">
\t \t \t <input type="search" class="search-field form-fluid" placeholder="search" value="" name="s" title="test">
\t \t </div>
</form>
このコードの皮をし、ボタンがクリックされたときに入力を示しています。問題は、入力を隠すためにボディをクリックしたときにもしたいのですが、これはボタンのクリックも検出し、入力をまったく表示しないので、入力は常に表示:なしです。私は間違って何をしているのですか?どうすればいいですか?
あなたは、ボタンをクリックした場合も、または本体のみで、チェックする必要があります。ボタンをクリックすると、ボタンも本体に属しているため、両方のonclick関数が呼び出されます。 – Danmoreng
ええ、私はそれを実現しましたが、私はそれをする方法を知らない。 – MilenD