gpt4 book ai didi

javascript - 修复 IE 占位符的 Javascript 错误

转载 作者:行者123 更新时间:2023-11-28 20:56:45 25 4
gpt4 key购买 nike

我最近coded a formJQuery Validation在里面。因为我在输入字段中使用 placeholder 方法,而 IE 并不完全支持这些方法,所以我使用 patch用于在 IE 中正确显示占位符。

这一切都有效,但是当你在 IE 中打开网站时,你会看到 JQuery 验证在我打开页面时立即被激活,这显然不是重点。所以我认为这是某种错误。我查看了 .js 文件,但我的 JS 技能不太好,所以我想知道这个错误可能是什么......
有人有想法吗?

最佳答案

<input type="text" placeholder="abc"/>

jQuery:

$( function() {
$('[placeholder]').on({

focus: function() {
if( $(this).val() == $(this).attr('placeholder') ) {
$(this).val('');
}
},

blur: function(){
if( !$(this).val() ) {
$(this).val( $(this).attr('placeholder') );
}

}

} ).each( function() {
$(this).val( $(this).attr('placeholder') ) ;
} );
});

运行它:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script>
<script type="text/javascript">
//script from above;
</script>
</head>
<body>
<input type="text" placeholder="abc"/>
</body>
</html>

关于javascript - 修复 IE 占位符的 Javascript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com