2017-09-01 69 views
2

我有一個登錄表單,當您將鼠標懸停在鏈接上時,它將顯示自己。基本上,當您懸停a時,表單將其規則更新爲display: nonedisplay: block懸停自動填充建議會使元素消失

當我開始填寫字段時,瀏覽器根據先前的輸入顯示建議。但是當我想要選擇建議時,element將自己重新設置爲display: none。我不想設置autocomplete="off"

建議顯示本身
enter image description here

所有形式消失盤旋建議
enter image description here

示範

* { 
 
    box-sizing: border-box; 
 
} 
 

 
form { 
 
    display: none; 
 
} 
 

 
div:hover form { 
 
    background-color: #f1f1f1; 
 
    display: block; 
 
    padding: 15px; 
 
}
<p>Fill in a sample username and password, after that you get an fiddle error. Just reload the fiddle and then the autocomplete shows you suggestions</p> 
 

 
<div class="hover"> 
 
    <span>HOVER TO SHOW LOGIN FORM</span> 
 
    <form method="post"> 
 
    <input type="text" id="username" name="username" placeholder="username" /> 
 
    <input type="password" id="password" name="password" placeholder="password" /> 
 
    <input type="submit" name="login" value="login" /> 
 
    </form> 
 
</div>

如何防止這種行爲?

+0

類將被添加你使用哪種瀏覽器? –

+0

你可以添加自動完成=「關閉」屬性 –

+0

所有瀏覽器同樣的問題。 IE,FF和Chrome。我不想設置自動完成=「關閉」 – Red

回答

2

次嘗試在我刪除懸停時,焦點輸入 a fiddle

$('input').blur(function() { 
    $('.wrapper').addClass('hover'); 
    }) 
    .focus(function() { 
    $('.wrapper').removeClass('hover'); 
    }); 

UPDATE
您可以添加鼠標離開也是如此,一旦懸停鼠標離開輸入
updated fiddle

$('input').blur(function() { 
    $('.wrapper').addClass('hover'); 
    }) 
    .mouseleave(function() { 
    $('.wrapper').addClass('hover'); 
    }) 
    .focus(function() { 
    $('.wrapper').removeClass('hover'); 
    }); 
+0

我excerly不想用jQuery來修復它。因爲即時通訊使用角度。但它似乎是它唯一的選擇:( – Red

+0

@紅我會嘗試找到一個普通的javascript解決方案 –

+0

沒有必要,我的意思是我只想用CSS實際上,我知道如何使用JavaScript修復它。使用其中任何一個但是,謝謝 – Red

0

這是一個簡單的例子,雖然你需要使用一點點的js,但我不認爲你沒有它可以解決這個問題。

爲了簡單起見,我在這裏使用了jquery,只是操縱了css屬性,但你也可以使用vanilla js並添加一個類或類似的東西。

我還包裹在另一格形式,而周圍的測試,你可能不一定需要是:

,我並沒有改變顯示回來時,你點擊別的地方,因爲說實話,那會是惱人的地獄,如果你已經點擊的形式和意外點擊別的地方,有它消失了....但是你可以添加,當然;)

$("form").click(()=> 
 
{ 
 
     $(".hideForm").css("display","block"); 
 
})
* { 
 
    box-sizing: border-box; 
 
} 
 

 
.hideForm { 
 
    display: none; 
 
} 
 

 
div:hover .hideForm{ 
 
    display:block; 
 
} 
 
form { 
 
    background-color: #f1f1f1; 
 
    display: block; 
 
    padding: 15px; 
 
} 
 
div form { 
 
    background-color: #f1f1f1; 
 
    display: block; 
 
    padding: 15px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>Fill in a sample username and password, after that you get an fiddle error. Just reload the fiddle and then the autocomplete shows you suggestions</p> 
 

 
<div class="hover"> 
 
    <span>HOVER TO SHOW LOGIN FORM</span> 
 
    <div class="hideForm"> 
 
<form method="post"> 
 
    <input type="text" id="username" name="username" placeholder="username" /> 
 
    <input type="password" id="password" name="password" placeholder="password" /> 
 
    <input type="submit" name="login" value="login" /> 
 
    </form> 
 
</div> 
 
</div>

+0

刪除光標時,表單不會被隱藏。 – Roberrrt

+0

是啊,這就是爲什麼我寫道「當我點擊其他地方時,我沒有改變顯示[...]」 – rebecca