摘要:屬性定義屬性規(guī)定可描述輸入字段預(yù)期值的簡(jiǎn)短的提示信息比如一個(gè)樣本值或者預(yù)期格式的短描述。問題來源屬性給予了用戶很友好的提示,但是在老版本的瀏覽器中就不會(huì)起作用及之前的版本不支持屬性,這是一個(gè)很頭疼的問題,于是就產(chǎn)生了以下的思考。
有關(guān)placeholder在ie9中的一點(diǎn)折騰。
placeholder屬性定義: placeholder 屬性規(guī)定可描述輸入字段預(yù)期值的簡(jiǎn)短的提示信息(比如:一個(gè)樣本值或者預(yù)期格式的短描述)。
解決思路問題來源: placeholder屬性給予了用戶很友好的提示,但是在老版本的瀏覽器中就不會(huì)起作用(Internet Explorer 9 及之前的版本不支持 placeholder 屬性),這是一個(gè)很頭疼的問題,于是就產(chǎn)生了以下的思考。
判斷瀏覽器是否支持placeholder屬性
"placeholder" in document.createElement("input")
獲取當(dāng)前頁面中的所有具有placeholder屬性的元素
document.querySelectorAll("[placeholder]")
由于document.querySelectorAll返回的是一個(gè) NodeList 對(duì)象,需要將其通過Array.prototype.slice.call()將其轉(zhuǎn)換成數(shù)組,這樣我們就可以通過數(shù)組的forEach()方法對(duì)頁面中獲取到的所有元素進(jìn)行遍歷
Array.prototype.slice.call(document.querySelectorAll("[placeholder]"))
對(duì)獲取到的頁面中的所有具有placeholder屬性的元素進(jìn)行遍歷
nodes.forEach()
根據(jù)當(dāng)前元素克隆出一個(gè)一樣的克隆節(jié)點(diǎn)(備注:這里的思想是這樣的,克隆出一個(gè)一樣的節(jié)點(diǎn)插入到當(dāng)前節(jié)點(diǎn)的后面,當(dāng)瀏覽器不支持placeholder屬性的時(shí)候,需要顯示placeholder屬性的信息,就將克隆節(jié)點(diǎn)顯示出來,將當(dāng)前節(jié)點(diǎn)隱藏掉)
var cloneNode = item.cloneNode()
判斷節(jié)點(diǎn)的類型,如果節(jié)點(diǎn)的類型[type="password"],就將克隆節(jié)點(diǎn)的類型改為text
if (cloneNode.getAttribute("type").toLowerCase() === "password") { cloneNode.setAttribute("type", "text") }
將克隆節(jié)點(diǎn)的placeholder屬性值,寫入value;并將此克隆節(jié)點(diǎn)隱藏
cloneNode.setAttribute("value", cloneNode.getAttribute("placeholder")) cloneNode.style.display = "none"
將克隆節(jié)點(diǎn)插入到當(dāng)前節(jié)點(diǎn)的后面
item.insertAdjacentHTML("afterend", cloneNode.outerHTML)
對(duì)克隆節(jié)點(diǎn)綁定focus事件,當(dāng)克隆節(jié)點(diǎn)獲取焦點(diǎn)時(shí),將克隆節(jié)點(diǎn)隱藏,并將當(dāng)前節(jié)點(diǎn)顯示出來,并將當(dāng)前節(jié)點(diǎn)獲取焦點(diǎn)
item.nextSibling.addEventListener("focus", function () { this.style.display = "none" this.previousSibling.style.display = "inline" this.previousSibling.focus() })
對(duì)當(dāng)前節(jié)點(diǎn)綁定focus事件,當(dāng)前節(jié)點(diǎn)獲取焦點(diǎn)時(shí),將緊跟在當(dāng)前節(jié)點(diǎn)后面的克隆節(jié)點(diǎn)隱藏掉
item.addEventListener("focus", function () { this.nextSibling.style.display = "none" })
對(duì)當(dāng)前節(jié)點(diǎn)綁定blur事件,當(dāng)前節(jié)點(diǎn)失去焦點(diǎn)時(shí),如果當(dāng)前節(jié)點(diǎn)沒有進(jìn)行任何輸入,則需要對(duì)齊進(jìn)行placeholder提示,這時(shí)就將當(dāng)前節(jié)點(diǎn)隱藏,將緊跟在當(dāng)前節(jié)點(diǎn)后面的克隆節(jié)點(diǎn)顯示出來
item.addEventListener("blur", function () { if (!this.value) { this.style.display = "none" this.nextSibling.style.display = "inline" } })
在頁面初始化完成后,判斷當(dāng)前節(jié)點(diǎn)是否有初始輸入值,如果沒有的話,將當(dāng)前節(jié)點(diǎn)隱藏,將緊跟在當(dāng)前節(jié)點(diǎn)后的克隆節(jié)點(diǎn)顯示出來
if (!item.value) { item.style.display = "none" item.nextSibling.style.display = "inline" }整體思路圖示 整體代碼
if (!("placeholder" in document.createElement("input"))) { // 將返回的nodeList對(duì)象轉(zhuǎn)為數(shù)組 var nodes = Array.prototype.slice.call(document.querySelectorAll("[placeholder]")) nodes.forEach(function (item, index) { item.addEventListener("focus", function () { this.nextSibling.style.display = "none" }) item.addEventListener("blur", function () { if (!this.value) { this.style.display = "none" this.nextSibling.style.display = "inline" } }) var cloneNode = item.cloneNode() // 如果[type="password"]類型,則轉(zhuǎn)為text if (cloneNode.getAttribute("type").toLowerCase() === "password") { cloneNode.setAttribute("type", "text") } cloneNode.setAttribute("value", cloneNode.getAttribute("placeholder")) cloneNode.style.display = "none" item.insertAdjacentHTML("afterend", cloneNode.outerHTML) item.nextSibling.addEventListener("focus", function () { this.style.display = "none" this.previousSibling.style.display = "inline" this.previousSibling.focus() }) if (!item.value) { item.style.display = "none" item.nextSibling.style.display = "inline" } }) }
由于本人學(xué)識(shí)有限,有很多需要提升的地方,望大家多多指教。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/91819.html
摘要:為此決定自研一個(gè)富文本編輯器。例如當(dāng)要轉(zhuǎn)化的對(duì)象有環(huán)存在時(shí)子節(jié)點(diǎn)屬性賦值了父節(jié)點(diǎn)的引用,為了關(guān)于函數(shù)式編程的思考作者李英杰,美團(tuán)金融前端團(tuán)隊(duì)成員。只有正確使用作用域,才能使用優(yōu)秀的設(shè)計(jì)模式,幫助你規(guī)避副作用。 JavaScript 專題之惰性函數(shù) JavaScript 專題系列第十五篇,講解惰性函數(shù) 需求 我們現(xiàn)在需要寫一個(gè) foo 函數(shù),這個(gè)函數(shù)返回首次調(diào)用時(shí)的 Date 對(duì)象,注意...
摘要:轉(zhuǎn)自偽類實(shí)現(xiàn)占位符交互效果一規(guī)范中占位符交互效果風(fēng)格占位符交互效果官方示意見此頁面。我們可以采用絕對(duì)定位最后,對(duì)這個(gè)元素在輸入框時(shí)候,以及非顯示的時(shí)候進(jìn)行重定位縮小并位移到上方四清除按鈕部分上是必要屬性,配合偽類實(shí)現(xiàn)我們的效果。 轉(zhuǎn)自: https://github.com/yougola/bl... CSS :placeholder-shown偽類實(shí)現(xiàn)Material Design占...
摘要:返回的是一個(gè)樣式聲明對(duì)象,只讀。方法返回一個(gè)最初值對(duì)象或值列表對(duì)象,這取決于屬性值的類型。而且,雖然中,對(duì)象支持方法,但總是返回因此,目前來講,方法可以先不聞不問。本文轉(zhuǎn)載之張?chǎng)涡竦牟┛? 一、getComputedStyle是?getComputedStyle是一個(gè)可以獲取當(dāng)前元素所有最終使用的CSS屬性值。返回的是一個(gè)CSS樣式聲明對(duì)象([object CSSStyleDeclara...
摘要:返回的是一個(gè)樣式聲明對(duì)象,只讀。方法返回一個(gè)最初值對(duì)象或值列表對(duì)象,這取決于屬性值的類型。而且,雖然中,對(duì)象支持方法,但總是返回因此,目前來講,方法可以先不聞不問。本文轉(zhuǎn)載之張?chǎng)涡竦牟┛? 一、getComputedStyle是?getComputedStyle是一個(gè)可以獲取當(dāng)前元素所有最終使用的CSS屬性值。返回的是一個(gè)CSS樣式聲明對(duì)象([object CSSStyleDeclara...
閱讀 1309·2021-11-24 09:39
閱讀 470·2019-08-30 14:12
閱讀 2677·2019-08-30 13:10
閱讀 2503·2019-08-30 12:44
閱讀 1021·2019-08-29 16:31
閱讀 938·2019-08-29 13:10
閱讀 2525·2019-08-27 10:57
閱讀 3216·2019-08-26 13:57