我可以在 queryselector() 中为 CONTAINS 使用属性选择器吗?

Can I use an attribute selector for CONTAINS in queryselector()?(我可以在 queryselector() 中为 CONTAINS 使用属性选择器吗?)
本文介绍了我可以在 queryselector() 中为 CONTAINS 使用属性选择器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想根据 href 属性的子字符串选择一个元素.

我最近发布了一个问题,我收到了一个基于开始于"的答案:

document.querySelector('#utility-menu a[href^="/email_check?lang="').textContent.trim();

但实际上,从一个页面到另一个页面的唯一常量是 href 属性中的 "lang=".所以 /email_check? 是特定于页面的,所以我不能在所有页面上使用这个变量.

是否可以修改我的选择器以返回任何 <a> 元素的 textContent,其中包含子字符串 "lang="?

解决方案

所有 CSS 选择器都记录在 MDN 并在 W3C CSSWG 选择器级别 4 概述中指定1 (存档链接).

你需要的是

#utility-menu a[href*="lang="]

<块引用>

<头>
模式代表
E[foo*=bar"]一个 E 元素,其 foo 属性值包含子字符串 bar.


1:CSSWG 目前的 CSS 选择器模块工作是选择器级别 4.并非所有浏览器都支持其所有功能.4 级草案包括从 1 级到 4 级的所有内容.观看级别"表列.今天的浏览器至少支持 Level 3,但请查看 兼容性表在 MDN 上确定.

I would like to select an element based on a substring of href attribute.

I posted a question recently where I received an answer based on "starts with":

document.querySelector('#utility-menu a[href^="/email_check?lang="').textContent.trim();

But actually, the only constant from page to page will be "lang=" within the href attribute. So /email_check? is page specific so I cannot use this variable on al pages.

Is it possible to modify my selector to return the textContent of any <a> element with the substring "lang=" within it?

解决方案

All CSS selectors are documented on MDN and specified in the W3C CSSWG Selectors Level 4 overview1 (Archived link).

The one you need is

#utility-menu a[href*="lang="]

Pattern Represents
E[foo*="bar"] An E element whose foo attribute value contains the substring bar.


1: The CSSWG’s current work of the CSS Selectors Module is Selectors Level 4. Not all of its features are supported in all browsers. The Level 4 draft includes everything from Level 1 to Level 4. Watch the "Level" table column. Today’s browsers support at least up to Level 3, but check the compatibility tables on MDN to be sure.

这篇关于我可以在 queryselector() 中为 CONTAINS 使用属性选择器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)