var placeholder=function($element){ var $ = window.jQuery; var version = parseFloat($.browser.version); var isIE8 = $.browser.msie && version < 9; var text = $element.attr("cm-placeholder") || $element.attr("placeholder") ; if (isIE8) { var $placeholderContainer = $element.closest("div"); $placeholderContainer.css({"position":"relative"}); window.setTimeout(function () { var getFakePlaceHolder = function () { var cmPlaceholderId = $element.attr("cm-placeholder-id"); var $fakePlaceholder; if (cmPlaceholderId && cmPlaceholderId.length > 0) { $fakePlaceholder = $placeholderContainer.find("#" + cmPlaceholderId); if ($fakePlaceholder && $fakePlaceholder.length > 0) { return $fakePlaceholder; } } var cm_placeholder_id = _.uniqueId("cm-placeholder-"); var fakePlaceholder = ‘<span id="‘ + cm_placeholder_id + ‘" class="cm-placeholder">‘ + text + ‘</span>‘; $fakePlaceholder = $(fakePlaceholder); $placeholderContainer.append($fakePlaceholder); $fakePlaceholder.on("click", function (e) { e.stopPropagation(); $element.focus(); }); $element.on(‘focus‘, function () { $fakePlaceholder.hide(); }).on(‘blur‘, function () { showFakePlaceholderIfNoText(); }); $element.attr("cm-placeholder-id", cm_placeholder_id); return $fakePlaceholder; }; var $fakePlaceholder = getFakePlaceHolder(); var showFakePlaceholderIfNoText = function () { if ($element.val() === ‘‘) { var ep = $element.position(); var top = ep.top; //- wp.top; var left = ep.left; //- wp.left; var height = $element.height() - 2; $fakePlaceholder.css({ ‘top‘: top + ‘px‘, ‘left‘: left + ‘px‘, "height": height + "px", "line-height": height + "px" }); $fakePlaceholder.show(); } else { $fakePlaceholder.hide(); } }; showFakePlaceholderIfNoText(); }, 0); } else { $element.attr("placeholder", text || ""); } }
时间: 2024-10-08 17:44:26