大佬教程收集整理的这篇文章主要介绍了如果字段留空,则Jquery返回模糊的原始值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我到目前为止所得到的. Jsfiddle Demo
这是我的代码
$(document).ready(function() { var field = $('input[type="text"]'); field.focus(function() { //Empty the field on focus var thisValue = $(this).val(); $(this).attr("value",""); }); field.blur(function() { //check the field if it is left empty if ($(this).val() == "") { //alert('This field can not be left empty'); $(this).val(thisvalue); } }); });
placeholder
attribute,它在所有主流浏览器中都是本机支持的.但是,旧版浏览器不支持它.要获得更广泛的支持,您需要对此属性进行补充.网上有许多选项可供您使用,但如果您愿意,可以自己动手.
基本上你想让自己和其他人使用标准标记:
<input name="fname" placeholder="First Name">
使用jQuery,您将响应具有占位符属性的任何元素的焦点和模糊(或focusin和focusout)事件.如果元素已聚焦并且具有占位符值,则清除该元素.如果元素模糊且为空,则提供占位符值.
这有点冗长,但我添加了注释以帮助遵循逻辑:
// Written and tested with jQuery 1.8.1 (function ( $) { // Play nice with jshint.com "use Strict"; // Abort if browser already supports placeholder if ( "placeholder" in document.createElement("input") ) { return; } // Listen at the document level to work with late-arriving elements $(document) // Whenever blur or focus arrises from an element with a placeholder attr .on("blur focus","[placeholder]",function ( event ) { // Determine the new value of that element $(this).val(function ( i,sVal ) { // First store a reference to it's placeholder value var placeholder = $(this).attr("placeholder"),newVal = sVal; // If the user is focusing,and the placehoder is already set if ( event.type === "focusin" && sVal === placeholder ) { // Empty the field newVal = ""; } // If the user is blurring,and the value is nothing but white space if ( event.type === "focusout" && !sVal.replace(/\s+/g,"") ) { // Set the placeholder newVal = placeholder; } // Return our new value return newVal; }); }) // Finally,when the document has loaded and is ready .ready(function () { // Find non-autofocus placeholder elements and blur them // This triggers the above logic,which may provide default values $(":input[placeholder]:not([autofocus])").blur(); }); }(jQuery));
此特定垫片仅提供基本功能.其他人可以在使用占位符值时扩展对更改字体颜色的支持,以及在开始键入之前保持占位符值可见(此方法只是在焦点时立即删除它).
以上是大佬教程为你收集整理的如果字段留空,则Jquery返回模糊的原始值全部内容,希望文章能够帮你解决如果字段留空,则Jquery返回模糊的原始值所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。