- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在写一个自定义<select>
使用原生 DOM 的元素(无 Polymer)。
我正在尝试将我的元素与 <label>
一起使用元素并在 <label>
时正确触发我的元素的点击事件被点击,即:
<label>
My Select:
<my-select placeholder="Please select one">...</my-select>
</label>
或
<label for='mySelect1'>My Select:</label>
<my-select id='mySelect1' placeholder="Please select one">...</my-select>
但是,即使我添加 tabindex
,这种行为似乎也不是开箱即用的。使其可聚焦。
这是代码的精简版本和 JSFiddle进行一些基本的调试:
var MySelectOptionProto = Object.create(HTMLElement.prototype);
document.registerElement('my-select-option', {
prototype: MySelectOptionProto
});
var MySelectProto = Object.create(HTMLElement.prototype);
MySelectProto.createdCallback = function() {
if (!this.getAttribute('tabindex')) {
this.setAttribute('tabindex', 0);
}
this.placeholder = document.createElement('span');
this.placeholder.className = 'my-select-placeholder';
this.appendChild(this.placeholder);
var selected = this.querySelector('my-select-option[selected]');
this.placeholder.textContent = selected
? selected.textContent
: (this.getAttribute('placeholder') || '');
};
document.registerElement('my-select', {
prototype: MySelectProto
});
最佳答案
But the standard is not finished, and maybe in the future you’ll be able to use the built-in semantic with autonomous custom elements, too.
我们现在生活在未来。标准发生了显着变化,我们现在拥有更强大的自定义元素。
<小时/>当与自定义元素关联的构造函数具有 formAssociated
时,标签才有效值为 true
的属性.
当查看documentation时乍一看,您可能会得出结论,不允许自定义元素,如 <label>
目标。文档说:
Elements that can be associated with a
<label>
element include<button>
,<input>
(except fortype="hidden"
),<meter>
,<output>
,<progress>
,<select>
and<textarea>
.
但是没有提到自定义元素。不管怎样,你试试你的运气,并尝试在 HTML validator 中输入这个 HTML 片段。 :
<label>Label: <my-custom-element></my-custom-element></label>
或者:
<label for="my-id">Label: </label><my-custom-element id="my-id"></my-custom-element>
这两个都是有效的 HTML 片段!
但是,尝试这个 HTML 片段:
<label for="my-id">Label: </label><span id="my-id"></span>
显示此错误消息:
Error: The value of the
for
attribute of thelabel
element must be the ID of a non-hidden form control.
但是为什么是随机的 <my-custom-element>
被认为是“非隐藏表单控件”?在 validator’s GitHub issues然后你会发现已经修复的 issue #963 :
label for
doesn’t allow custom elements as targetsThe spec allows form-associated custom elements as
for
targets
https://html.spec.whatwg.org/multipage/forms.html#attr-label-forThis currently triggers an error:
<label for="mat-select-0">Select:</label>
<mat-select role="listbox" id="mat-select-0"></mat-select>Easy to check if element name contains a dash.Much harder to check that custom element definition includes a
static formAssociated
property returningtrue
, since this is likely buried deep in an external framework.js
file:
https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-face-example
WHATWG specification 中确实有一个特殊的附录。它枚举了与文档 plus form-associated custom elements 相同的“可标记元素” 。有一个example在规范中,今天研究这个问题在结果中产生了这篇问答文章以及博客文章,例如 “More capable form controls” by Arthur Evans ( archived link ),显示解决方案是什么。
<label>
的解决方案单击定位自定义元素:formAssociated
启用 <label>
所需执行的所有操作。操作是添加 formAssociated
值为 true
的属性到自定义元素的构造函数。
但是问题中的代码需要移植到当前的Web Components API 。新formAssociated
属性是新的 ElementInternals API 的一部分(请参阅 attachInternals
),用于更丰富的表单元素控制和可访问性。
现在,自定义元素是用 classes
定义的和 customElements.define
,和 ShadowRoots被使用。static formAssociated = true;
然后可以将字段添加到您认为属于“表单关联的自定义元素”的类中。拥有此属性可以使您的自定义元素分派(dispatch) click
单击 <label>
时发生的事件;现在我们已经取得进展了!
为了把这个click
进入focus
行动,我们还需要 attachShadow
中的一件事调用:属性(property)delegatesFocus
设置为true
.
// Rough translation of your original code to modern code, minus the `tabIndex` experimentation.
class MySelectOptionProto extends HTMLElement{}
customElements.define("my-select-option", MySelectOptionProto);
class MySelectProto extends HTMLElement{
#shadowRoot;
#internals = this.attachInternals();
static formAssociated = true;
constructor(){
super();
this.#shadowRoot = this.attachShadow({
mode: "open",
delegatesFocus: true
});
this.#shadowRoot.replaceChildren(Object.assign(document.createElement("span"), {
classList: "my-select-placeholder"
}));
this.#shadowRoot.firstElementChild.textContent = this.querySelector("my-select-option[selected]")?.textContent ?? (this.getAttribute("placeholder") || "");
}
}
customElements.define("my-select", MySelectProto);
<label>My label:
<my-select placeholder="Hello">
<my-select-option>Goodbye</my-select-option>
<my-select-option>world</my-select-option>
</my-select>
</label>
<label for="my-id">My label:</label>
<my-select id="my-id">
<my-select-option>Never gonna give you</my-select-option>
<my-select-option selected>up</my-select-option>
</my-select>
<label for="my-id">My other label</label>
delegatesFocus
当单击自定义元素的某些不可聚焦部分时,将聚焦影子根的第一个可聚焦子级。
如果您需要更多控制,可以删除该属性,并向自定义元素实例添加事件监听器。为了确保点击来自<label>
,您可以检查事件是否为target
是您的自定义元素,即 this
,屏幕上 x 和 y 位置的元素是 <label>
定位您的自定义元素。幸运的是,通过传递 x
可以非常可靠地完成此操作。和 y
至 document.elementFromPoint
。然后,只需调用 focus
在您想要的元素上即可完成工作。
针对自定义元素的标签列表是 ElementInternals API 通过 labels
提供的另一件事。属性。
class MySelectProto extends HTMLElement{
// …
constructor(){
// …
this.addEventListener("click", ({ target, x, y }) => {
const relatedTarget = document.elementFromPoint(x, y);
if(target === this && new Set(this.#internals.labels).has(relatedTarget)){
console.log("Label", relatedTarget, "has been clicked and", this, "can now become focused.");
// this.focus(); // Or, more likely, some target within `this.#shadowRoot`.
}
});
}
}
关于javascript - 单击标签不聚焦自定义元素(Web 组件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38320270/
我有一个登录框,当 出现时,它会上下滑动/切换。单击链接。 这是一个尺寸约为 150 像素 x 100 像素的 DIV。 当有人点击此 DIV 外部时,我希望 DIV 滑动备份。我一直在玩 focu
jQuery 会阻止这种类型的行为吗? $("input").focusout(function() { $(this).focus(); }); 最佳答案 这是如何完成的(跨浏览器工作):
我想将聚焦和未聚焦窗口的 Alpha 设置为 90% 和 70%,而不是使用边框。这将适用于所有窗口,除了那些匹配某些标准(例如 className =“Gimp”)的窗口。我如何在我的 xmonad
我的布局中有一个 Recyclerview 和一个 EditText。 当我在 EditText 外部单击(即单击 Recyclerview)时,我希望 Recyclerview 获得焦点,而 Edi
我正在开发一个使用旧版 jQuery 1.3.2 的元素。我需要在 focus 上 blur 输入值并在 focusout 上返回它(如果没有改变)(不能使用 placeholder在这里)。 这就是
我有一个带有几个 TextViews 的自定义抽屉导航。这是布局: 问题是,如果我没有为 TextView
是否有一种简单的方法可以使我的应用程序的 float 工具选项板永久处于事件状态?它们都与打开的文档相关,因此没有理由让它们在任何时间点处于不活动状态。 最佳答案 使用 NSPanel 作为实用程序窗
尽管阅读了我可以在developer.android.com和stackoverflow上获得的所有内容,但是我确实认为应该可以实现,但是我仍然不知道该怎么做。 我正在尝试使用OpenGL ES 2.
我想在页面加载时聚焦一个元素( slider /轮播),以允许使用箭头键立即控制 slider 。 JQuery 的 .focus() 可以工作,但会自动将元素滚动到 View 中。这是有限视口(vi
我正在尝试让我的代码针对 Mozzila 中 14 年未修复的错误,该错误无法重新关注输入。我在多个地方发现(google mozilla focus not working 中的前 20 个结果中的
我有 ViewController,我想通过焦点引擎检索控制权。首先,我尝试通过要求自己查看来启动。 if settingsFilterSegment.isFocused {} 但我变得错误了,所以我
我想以编程方式将焦点设置在连续一列的表格中 NSTableCellView 中的两个编辑字段之一上,以便用户可以立即编辑该字段。在我的示例中,我可以选择该行(按下按钮后),但我找不到将焦点设置在特殊字
我创建了一个小的 Tab-Layout。 现在我如何根据 TabWidget 的状态(聚焦、按下...)指定它的颜色 我构建了一个新的 .xml,但我不知道如何将它设置到我的选项卡:
我有一个带有 JTree 的 JDialog,其中包括 DefaultMutableTreeNode 及其子节点。我希望在显示 JDialog 时聚焦树的节点之一。只需使用 tree.requestF
当我单击 EditText 时,会出现软键盘。我不希望在单击 EditText 时出现键盘,但是,我想获得 EditText 的力量。我想在没有键盘的情况下编辑它。 editText.setOnCli
在下图中,您可以看到“>”符号周围有一个虚线的“内部”边框,表示该按钮已获得焦点。 (我还在按钮周围添加了一个蓝色边框) 在我构建这些按钮的代码中,我正在做: setTimeout(function(
我在 css 上相当“新手”,并试图在主 div 聚焦时更改 FontAwesome 图标的颜色。我尝试了几种方法,尝试使用 div 的 id 或 class 在 focus 上更改它的颜色,但都没有
我如何判断焦点事件是由于输入按下=表单提交而发生还是仅仅因为单击而发生?进入控制台的事件数据是“focusout”类型,没有相关信息 $(".clientrow[clientid="+clientid
我有个小问题。目前,我正在使用 WordPress 开发一个带有 Flash 游戏的简单网页。 例如考虑这个页面:http://ggames.eu/2015/11/25/prince-of-persi
A 有一个 ListBox,需要在特定情况下聚焦特定项目(例如,显示搜索结果时)。这是通过附加属性使用以下函数完成的: public static void OnElementIndexPropert
我是一名优秀的程序员,十分优秀!