它已經一段時間,因爲這個問題被問。然而我碰到它想要做同樣的事情。看來v0和v1的規格現在很亂。儘管使用HTMLButtonElement的代碼相同,但它使用HTMLSelectElement的方式將非法構造函數置於chrome中。我已經開始寫這篇文章,並認爲我會分享。它可以被清理並添加更多的select元素方法,但是可以指望其他人可以從這裏採取它來根據他們的需求進行定製。
class mySelectElement extends HTMLElement {
static get observedAttributes() { return ['disabled']; }
constructor() {
super();
let shadowRoot = this.attachShadow({
mode: 'open'
}),
content = document.createElement('slot'),
options = null;
content.setAttribute('select', 'option');
shadowRoot.innerHTML = `<style>
:host([disabled]) {
background: grey;
pointer-events: none;
opacity: 0.4;
pointer-events: none;
height: 16px;
}
:host:before{
content: '';
}
:host{
contain: layout size style;
overflow: auto;
align-items:center;
background-color:rgb(255, 255, 255);
border: 1px solid black;
color:rgb(0, 0, 0);
display:inline-block;
font: 13.3333px Arial;
height:16px;
width:145px;
writing-mode:horizontal-tb;
-webkit-appearance:menulist;
-webkit-rtl-ordering:logical;
}
.hide{
display:none;
}
#options{
position: fixed;
border:1px solid blue;
}
::slotted(option){
background-color:white;
}
::slotted(:hover){
background-color: #a4d8d2;
}
</style>
<div id="options" class="hide"></div>`;
options = shadowRoot.getElementById('options');
options.appendChild(content);
this.disabled = false;
this.setAttribute('tabIndex', -1);
this.addEventListener('click', function (e) {
let target = e.target;
if (target.nodeName == 'OPTION') {
this.value = target.value;
Array.from(target.parentElement.children).forEach(x => x.removeAttribute('selected'));
target.setAttribute('selected', '');
shadowRoot.styleSheets[0].rules[1].style.cssText = "content: "
+ '"' + target.textContent + '"';
this.blur();
}
});
this.addEventListener('focus', function() {
let rect = this.getBoundingClientRect();
options.style.top = rect.bottom;
options.style.left = rect.left;
options.style.width = rect.width;
options.classList.remove('hide');
});
this.addEventListener('focusout', function() {
options.classList.add('hide');
});
this.add = function (item) {
this.appendChild(item);
if (this.value == undefined) {
this.value = content.assignedNodes()[0].value;
content.assignedNodes()[0].setAttribute('selected', '');
shadowRoot.styleSheets[0].rules[1].style.cssText = "content: " +
'"' + content.assignedNodes()[0].textContent + '"';
}
}
this.item = function (i) {
return content.assignedNodes()[i];
}
this.namedItem = function (val) {
return content.assignedNodes().find(x => x.value == val);
}
this.remove = function (i) {
return content.assignedNodes()[i].remove();
}
}
attributeChangedCallback(attributeName, oldValue, newValue, namespace) {
if (attributeName == 'disabled') {
if (newValue = '')
this.disabled = true;
else if (newValue == null)
this.disabled = false;
}
}
}
customElements.define('my-select', mySelectElement);
var _select = customElements.get('my-select');
var select = new _select;
document.body.appendChild(select);
for (let i = 0; i < 10; i++) {
let option = document.createElement('option');
option.innerHTML = 'hello_' + i;
option.value = 'h' + i;
select.add(option);
}
沒有那不是我想要什麼,我想寫: <卡斯特 - 選擇選項> .... .... –
我想卡斯特選到表現就像
我猜,那麼在計劃HTML/Vanilla javascript中實現它將是相當不可能的。要麼你需要採用像[鏈接](https://www.polymer-project.org/1.0/)聚合物項目[鏈接]的框架,否則你可以按照上面的例子**