- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个组合框,为此,我想使用新的 :focus-within
伪选择器来管理与组合框关联的可扩展列表框的显示。
:focus-within 是一个很好的解决方案,并且很有魅力。我遇到的唯一问题是弄清楚如何保持列表框中的 aria-expanded 属性更新。由于伪选择器的存在,所有隐藏/显示功能都发生在浏览器区域,因此我不确定是否可以在 Javascript 中使用 Hook 来确定元素何时可见或不可见,以便更新该属性。
有没有人知道的优雅解决方案?我不想复制 :focus-within 现在正在处理的焦点/模糊逻辑,只是为了更新此属性。我还担心他们可能会不同步。有什么方法可以使用 :focus-within
或类似的东西来监视元素吗?
.list {
display: none;
}
.combobox-container:focus-within .list {
display: block;
}
<section class="combobox-container">
<div role="combobox" aria-expanded="false" aria-owns="listbox" aria-haspopup="listbox">
<label> Foo
<input type="text" aria-autocomplete="list" aria-controls="listbox" />
</label>
</div>
<ul class="list" id="listbox" role="listbox" tabindex="0" aria-multiselectable="true">
<!-- items for autocomplete. focusable anchors inside li tags. -->
<li><a href="#">Javascript</a></li>
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
</ul>
</section>
最佳答案
focus-within only has 84% browser coverage
因此,这会立即导致您的解决方案无法访问,因为许多屏幕阅读器用户仍在使用 JAWs
使用 Internet Explorer。
此外,您还有一个问题,虽然这作为演示,但在现实世界中,将通过 AJAX 或通过过滤的预加载列表填充自动完成列表。
这意味着列表将始终在您聚焦 <input>
时显示。 ,即使没有在组合框中输入任何内容(这不是预期的行为)。
这是少数可以接受仅依赖 JavaScript 的情况之一(但仍可以在没有 JavaScript 的情况下提交表单)。
而不是尝试使用 :focus-within
您可以使用 JavaScript 来切换 aria-expanded="true"
当您返回一些建议然后使用standard CSS3 selectors时显示和隐藏结果。
下面的示例显示了实现此目的的 CSS。 +
运算符是关键,它是Adjacent Sibling Combinator选择父元素中的下一个兄弟元素。
CSS: .combobox-container div[aria-expanded="true"]+.list
对于下面的示例,我这样做是为了一旦您在框中输入超过 1 个字符,它就会更改 aria-expanded
属性为true
(如果输入为空则再次返回)-这使它感觉更像是“现实世界”的示例。
旁注:您无需添加 tabindex
到<ul>
,预期的行为是直接跳到第一个建议的元素,我在下面的示例中删除了它。
//ignore this, this is my standard jQuery replacement for snippets
if(typeof $=="undefined"){!function(b,c,d,e,f){f=b['add'+e]
function i(a,d,i){for(d=(a&&a.nodeType?[a]:''+a===a?b.querySelectorAll(a):c),i=d.length;i--;c.unshift.call(this,d[i]));}
$=function(a){return /^f/.test(typeof a)?/in/.test(b.readyState)?setTimeout(function(){$(a);},9):a():new i(a);};$[d]=i[d]={on:function(a,b){return this.each(function(c){f?c['add'+e](a,b,false):c.attachEvent('on'+a,b)})},off:function(a,b){return this.each(function(c){f?c['remove'+e](a,b):c.detachEvent('on'+a,b)})},each:function(a,b){for(var c=this,d=0,e=c.length;d<e;++d){a.call(b||c[d],c[d],d,c)}
return c},splice:c.splice}}(document,[],'prototype','EventListener');var props=['add','remove','toggle','has'],maps=['add','remove','toggle','contains'];props.forEach(function(prop,index){$.prototype[prop+'Class']=function(a){return this.each(function(b){if(a){b.classList[maps[index]](a);}});};});$.prototype.hasClass=function(a){return this[0].classList.contains(a);};}
$.prototype.find=function(selector){return $(selector,this);};$.prototype.parent=function(){return(this.length==1)?$(this[0].parentNode):[];};$.prototype.findWithin=function(a){console.log("THIS IS",this[0],a);return this[0].getElementsByClassName(a);};$.prototype.first=function(){return $(this[0]);};$.prototype.focus=function(){return this[0].focus();};$.prototype.css=function(a,b){if(typeof(a)==='object'){for(var prop in a){this.each(function(c){c.style[prop]=a[prop];});}
return this;}else{return b===[]._?this[0].style[a]:this.each(function(c){c.style[a]=b;});}};$.prototype.text=function(a){return a===[]._?this[0].textContent:this.each(function(b){b.textContent=a;});};$.prototype.html=function(a){return a===[]._?this[0].innerHTML:this.each(function(b){b.innerHTML=a;});};$.prototype.attr=function(a,b){return b===[]._?this[0].getAttribute(a):this.each(function(c){c.setAttribute(a,b);});};$.param=function(obj,prefix){var str=[];for(var p in obj){var k=prefix?prefix+"["+p+"]":p,v=obj[p];str.push(typeof v=="object"?$.param(v,k):encodeURIComponent(k)+"="+encodeURIComponent(v));}
return str.join("&");};$.prototype.append=function(a){return this.each(function(b){b.appendChild(a[0]);});};$.ajax=function(a,b,c,d){var xhr=new XMLHttpRequest();var type=(typeof(b)==='object')?1:0;var gp=['GET','POST'];xhr.open(gp[type],a,true);if(type==1){xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");}
xhr.responseType=(typeof(c)==='string')?c:'';var cb=(!type)?b:c;xhr.onerror=function(){cb(this,true);};xhr.onreadystatechange=function(){if(this.readyState===4){if(this.status>=200&&this.status<400){cb(this,false);}else{cb(this,true);}}};if(type){xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');xhr.send($.param(b));}else{xhr.send();}
xhr=null;};
//only part of the demo, not for production use
$('input').on('keyup', function(e){
if($(this)[0].value.length > 0){
$('div[role=combobox]').attr('aria-expanded', true);
return;
}
$('div[role=combobox]').attr('aria-expanded', false);
return;
});
.list {
display: none;
}
.combobox-container div[aria-expanded="true"]+.list{
display: block;
border:2px solid #333;
}
<section class="combobox-container">
<div role="combobox" aria-expanded="false" aria-owns="listbox" aria-haspopup="listbox">
<label> Foo
<input type="text" aria-autocomplete="list" aria-controls="listbox" />
</label>
</div>
<ul class="list" id="listbox" role="listbox" aria-multiselectable="true">
<!-- items for autocomplete. focusable anchors inside li tags. -->
<li><a href="#">Javascript</a></li>
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
</ul>
</section>
关于javascript - 使用 :focus-within 时更新 aria-expanded 属性的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60198253/
我有一个简单的 Card 组件,是从material-ui 的网站复制的。 我正在尝试在我的代码中实现它。当我单击 CardHeader 时,它不会展开。 这是我的组件: import React,
我已经使用延迟加载实现了一棵树。第一级节点是在树创建时创建的,其中只有当用户展开任何特定节点时才会创建子节点。 数据来自数据库,我们向数据库发出查询以填充子节点。实现了 TreeExpansionLi
假设我有 3 个向量(仅作为示例)。现在我想获取这 3 个所有可能组合的随机样本。通常,我会这样做: x <- 1:3 y <- 10:12 z <- 15:18 N <- length(x) * l
如果我使用 dabbrev-expand为了扩展,Emacs 搜索当前缓冲区,然后搜索其他具有相同模式的缓冲区。这是由 dabbrev-friend-buffer-function 处理的默认设置为
我想像这样切片主窗口 我的布局代码如下: QGridLayout *gLayout = new QGridLayout (); viewWidget->setStyleSheet("backgroun
我在 Android Studio Canary 1 上尝试 Jetpack Compose 并添加了 Column可组合到 ui。 Column有一个名为 modifier 的属性我们可以在其中传递
我正在努力让我们的 Accordion 可以使用 aria-expanded 等 aria 标签来访问。当单击 Accordion 触发器标题或按下键盘上的“返回”按钮时,我正确地更改了 aria-e
根据 http://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum ,设置小部件的大小策略具有以下效果: The sizeHint() is a sensibl
我将使用Live Actitions显示实时数据,并在一个应用程序中添加对Dynamic Island的支持,该应用程序具有现有的小部件扩展。然而,我也不能把它造出来。当我收到错误信息时。和。即使我提
我有一个设置,如下所示: //With dynamic content here. 我正在运行一个脚本,该脚本将 #nav 的大小调整为浏览器窗口的高度大小。但有时我的
我正在使用jquery checkboxtree plugin它效果很好,并且上面的链接中有很好的文档和示例。我现在遇到一种情况,我想以编程方式检查节点。使用以下语法支持此操作: $('#tabs-
我正在尝试以下实验: 我有两个QpushButtons,比如PushA 和PushB。现在 PushA 在 QHBoxLayout 中,PushB 也在它自己的 QHBoxLayout 中。这两个水平
我目前有一个 OData V4 服务,它具有以下模型。 “类别”——“代码” 对于每个类别,可以有许多代码。 我需要 $expand the Codes, $filter where Active =
我的目的是创建一个带有 QVBoxLayout 的可滚动控件,上面有各种控件(比如按钮)。该控件放在 *.ui 窗体上。在该控件的构造函数中,我编写了以下代码: MyScrollArea::M
你们如何解决以下 Flutter 布局? 我有一个屏幕,我必须在其中显示,如图所示:一个 Logo + 3 个 TextFormFields + 2 个按钮 + 一个容器。 问题: 我需要将所有小部件
我使用最新版本的 mvvm light 工具包,但是我不清楚如何将 EventToCommand 用于事件 TreeViewItem.Expanded。 这很有效......我做错了什么?
我是 LINQ 的新手。 我有以下查询,我不知道它代表什么。 var query = (from p in data.First
这里有一个小问题。我有一个表,当我想单击运行时,该表应该展开,而且同一行中有一个展开按钮,它也应该展开一个 div。 问题:当我单击该行时,一切正常,div 将滑入。当我单击按钮(位于表行中)时,会产
图片:
我的应用程序中有 Expander 设置 FlowDirection 正常工作,但标题文本显示在水平方向。我想显示标题文本垂直绘制。 最佳答案 使用 sl 工具包中的 LayoutTransforme
我是一名优秀的程序员,十分优秀!