CSS attr灞炴€ч€夋嫨鍣ㄥ疄鐜板垪琛ㄨ繃婊ゆ晥鏋滃疄渚嬮〉闈?/h1>
鍥炲埌鐩稿叧鏂囩珷 »
浠g爜锛?/h3>
CSS浠g爜锛?/h5>
.search {
width: 180px;
padding: 5px;
-webkit-box-sizing: content-box;
}
.datalist {
display: block;
width: 190px;
background-color: #fff;
box-shadow: 0 1px #ccc, 1px 0 #ccc, -1px 0 #ccc, 0 -1px #ccc;
overflow: hidden;
visibility: hidden;
}
.search:focus + .datalist {
visibility: visible;
}
...
HTML浠g爜锛?/h5>
<input type="search" class="search" id="city" placeholder="杈撳叆鐪佷細鎴栫洿杈栧競鍚嶇О" />
<label class="datalist" for="city">
<div class="list" data-index="閲嶅簡甯俢hongqing">閲嶅簡甯?lt;/div>
<div class="list" data-index="鍝堝皵婊ㄥ競haerbing">鍝堝皵婊ㄥ競</div>
<div class="list" data-index="闀挎槬甯俢hangchun">闀挎槬甯?lt;/div>
...
</label>
JS浠g爜锛?/h5>
var eleStyle = document.createElement("style"),
eleInput = document.querySelector("#city");
// 鐢ㄦ潵CSS鎺у埗鐨剆tyle鎻掑叆
document.querySelector("head").appendChild(eleStyle);
// 鏂囨湰妗嗚緭鍏?eleInput.addEventListener("input", function() {
var val = this.value.trim().toLowerCase();
if (val !== '') {
eleStyle.innerHTML = '.list:not([data-index*="'+ this.value +'"]) { display: none; }';
} else {
eleStyle.innerHTML = '';
}
});
鏁堟灉锛?/h3>
浠g爜锛?/h3>
CSS浠g爜锛?/h5>
.search {
width: 180px;
padding: 5px;
-webkit-box-sizing: content-box;
}
.datalist {
display: block;
width: 190px;
background-color: #fff;
box-shadow: 0 1px #ccc, 1px 0 #ccc, -1px 0 #ccc, 0 -1px #ccc;
overflow: hidden;
visibility: hidden;
}
.search:focus + .datalist {
visibility: visible;
}
...
HTML浠g爜锛?/h5>
<input type="search" class="search" id="city" placeholder="杈撳叆鐪佷細鎴栫洿杈栧競鍚嶇О" />
<label class="datalist" for="city">
<div class="list" data-index="閲嶅簡甯俢hongqing">閲嶅簡甯?lt;/div>
<div class="list" data-index="鍝堝皵婊ㄥ競haerbing">鍝堝皵婊ㄥ競</div>
<div class="list" data-index="闀挎槬甯俢hangchun">闀挎槬甯?lt;/div>
...
</label>
JS浠g爜锛?/h5>
var eleStyle = document.createElement("style"),
eleInput = document.querySelector("#city");
// 鐢ㄦ潵CSS鎺у埗鐨剆tyle鎻掑叆
document.querySelector("head").appendChild(eleStyle);
// 鏂囨湰妗嗚緭鍏?eleInput.addEventListener("input", function() {
var val = this.value.trim().toLowerCase();
if (val !== '') {
eleStyle.innerHTML = '.list:not([data-index*="'+ this.value +'"]) { display: none; }';
} else {
eleStyle.innerHTML = '';
}
});
CSS浠g爜锛?/h5>
.search {
width: 180px;
padding: 5px;
-webkit-box-sizing: content-box;
}
.datalist {
display: block;
width: 190px;
background-color: #fff;
box-shadow: 0 1px #ccc, 1px 0 #ccc, -1px 0 #ccc, 0 -1px #ccc;
overflow: hidden;
visibility: hidden;
}
.search:focus + .datalist {
visibility: visible;
}
...
HTML浠g爜锛?/h5>
<input type="search" class="search" id="city" placeholder="杈撳叆鐪佷細鎴栫洿杈栧競鍚嶇О" />
<label class="datalist" for="city">
<div class="list" data-index="閲嶅簡甯俢hongqing">閲嶅簡甯?lt;/div>
<div class="list" data-index="鍝堝皵婊ㄥ競haerbing">鍝堝皵婊ㄥ競</div>
<div class="list" data-index="闀挎槬甯俢hangchun">闀挎槬甯?lt;/div>
...
</label>
JS浠g爜锛?/h5>
var eleStyle = document.createElement("style"),
eleInput = document.querySelector("#city");
// 鐢ㄦ潵CSS鎺у埗鐨剆tyle鎻掑叆
document.querySelector("head").appendChild(eleStyle);
// 鏂囨湰妗嗚緭鍏?eleInput.addEventListener("input", function() {
var val = this.value.trim().toLowerCase();
if (val !== '') {
eleStyle.innerHTML = '.list:not([data-index*="'+ this.value +'"]) { display: none; }';
} else {
eleStyle.innerHTML = '';
}
});
<input type="search" class="search" id="city" placeholder="杈撳叆鐪佷細鎴栫洿杈栧競鍚嶇О" /> <label class="datalist" for="city"> <div class="list" data-index="閲嶅簡甯俢hongqing">閲嶅簡甯?lt;/div> <div class="list" data-index="鍝堝皵婊ㄥ競haerbing">鍝堝皵婊ㄥ競</div> <div class="list" data-index="闀挎槬甯俢hangchun">闀挎槬甯?lt;/div> ... </label>