我可以在 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 使用属性选择器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Using discord.js to detect image and respond(使用 discord.js 检测图像并响应)
Check if user ID exists in Discord server(检查 Discord 服务器中是否存在用户 ID)
Guild Member Add does not work (discordjs)(公会成员添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何为我的 Discord.js 机器人编写事件/命令处理程序?)
How to find a User ID from a Username in Discord.js?(如何从 Discord.js 中的用户名中查找用户 ID?)