亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
Why CSS Can't Do This
Workarounds and Alternatives
1. Use Classes or Attributes Instead
2. Use :has() with Text (Limited Support)
3. Use JavaScript When Necessary
Summary
首頁 web前端 css教學(xué) 如何通過CSS中的文本內(nèi)容選擇一個(gè)元素?

如何通過CSS中的文本內(nèi)容選擇一個(gè)元素?

Jul 26, 2025 am 09:12 AM
css選擇器 文本內(nèi)容

?純CSS無法根據(jù)文本內(nèi)容選擇元素,因?yàn)镃SS標(biāo)準(zhǔn)不支持基於文本內(nèi)容的選器如:contains();?應(yīng)使用類或?qū)傩詠砭珳?zhǔn)定位元素,這是語義化且高效的做法;?若必須按文本選擇,可藉助JavaScript或jQuery實(shí)現(xiàn);??儘管現(xiàn)代瀏覽器支持:has()偽類,但它不能直接匹配純文本內(nèi)容,僅能通過子元素或?qū)傩蚤g接匹配,因此合理使用HTML結(jié)構(gòu)與樣式分離原則才是最佳實(shí)踐。

How to select an element by its text content in CSS?

You can't directly select an element by its text content using pure CSS .

How to select an element by its text content in CSS?

CSS is designed to style elements based on attributes like tag names, classes, IDs, states (like :hover ), and structural relationships — but it does not have a selector that matches elements based on their inner text (like "find me the <p></p> that contains 'Hello World'").


Why CSS Can't Do This

CSS lacks a selector such as :contains("text") in standard specifications. While jQuery has a :contains() selector, it's not part of standard CSS and won't work in regular stylesheets.

How to select an element by its text content in CSS?

Example (this won't work in CSS):

 /* ? Invalid in standard CSS */
p:contains("Submit") {
  background: yellow;
}

Workarounds and Alternatives

1. Use Classes or Attributes Instead

The best practice is to add a class, data attribute, or ID to the element you want to target.

How to select an element by its text content in CSS?
 <button class="btn-submit">Submit</button>
 .btn-submit {
  background: blue;
  color: white;
}

This is clean, efficient, and maintainable.


2. Use :has() with Text (Limited Support)

In some modern browsers (like Chrome 105 ), you can use the * ` :has()` selector with certain conditions. However, you still can't match exact text content* , but you can* check for elements that contain specific child elements or attributes.

But if the text is inside a child (eg, via ::before with content ), you can sometimes work around it:

 /* Example: Match a label that contains a span with text */
label:has(span[aria-label="required"]) {
  font-weight: bold;
}

Still, this doesn't help for direct text content matching.


3. Use JavaScript When Necessary

If you absolutely need to select an element by its text, use JavaScript:

 // Find a button with the text "Submit"
const submitButton = [...document.querySelectorAll(&#39;button&#39;)]
  .find(btn => btn.textContent.trim() === &#39;Submit&#39;);

if (submitButton) {
  submitButton.style.backgroundColor = &#39;yellow&#39;;
}

Or with :contains in jQuery (if you're using it):

 $(&#39;p:contains("Hello")&#39;).css(&#39;color&#39;, &#39;red&#39;);

Summary

  • ? No native CSS selector can select elements by text content.
  • ? Use classes or attributes — it's the proper semantic way.
  • ? Use JavaScript or jQuery for dynamic text-based selection.
  • ?? :has() is powerful but doesn't solve text-content matching directly.

So while the answer might feel limiting, relying on proper HTML structure with meaningful classes is usually cleaner and more performant anyway.

以上是如何通過CSS中的文本內(nèi)容選擇一個(gè)元素?的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
如何調(diào)整HTML文字方塊的大小 如何調(diào)整HTML文字方塊的大小 Feb 20, 2024 am 10:03 AM

HTML文字方塊大小的設(shè)定在前端開發(fā)中是非常常見的操作。本文將介紹如何設(shè)定文字方塊的尺寸,並提供具體的程式碼範(fàn)例。在HTML中,可以使用CSS來設(shè)定文字方塊的尺寸。具體的程式碼如下:input[type="text&quot

H5頁面製作究竟指什麼 H5頁面製作究竟指什麼 Apr 06, 2025 am 07:18 AM

H5 頁面製作是指使用 HTML5、CSS3 和 JavaScript 等技術(shù),創(chuàng)建跨平臺兼容的網(wǎng)頁。其核心在於瀏覽器解析代碼,渲染結(jié)構(gòu)、樣式和交互功能。常見技術(shù)包括動(dòng)畫效果、響應(yīng)式設(shè)計(jì)和數(shù)據(jù)交互。為避免錯(cuò)誤,應(yīng)使用開發(fā)者工具調(diào)試;而性能優(yōu)化和最佳實(shí)踐則包括圖像格式優(yōu)化、減少請求和代碼規(guī)範(fàn)等,以提高加載速度和代碼質(zhì)量。

如何調(diào)整WordPress主題避免錯(cuò)位顯示 如何調(diào)整WordPress主題避免錯(cuò)位顯示 Mar 05, 2024 pm 02:03 PM

如何調(diào)整WordPress主題避免錯(cuò)位顯示,需要具體程式碼範(fàn)例WordPress作為一個(gè)功能強(qiáng)大的CMS系統(tǒng),受到了許多網(wǎng)站開發(fā)者和站長的喜愛。然而,在使用WordPress建立網(wǎng)站時(shí),經(jīng)常會遇到主題錯(cuò)位顯示的問題,這對於使用者體驗(yàn)和頁面美觀都會造成影響。因此,合理調(diào)整WordPress主題以避免錯(cuò)位顯示是非常重要的。本文將介紹如何透過具體的程式碼範(fàn)例來進(jìn)行主題調(diào)

H5頁面製作的流程 H5頁面製作的流程 Apr 06, 2025 am 09:03 AM

H5頁面製作流程:設(shè)計(jì):規(guī)劃頁面佈局、風(fēng)格和內(nèi)容;HTML結(jié)構(gòu)搭建:使用HTML標(biāo)籤構(gòu)建頁面框架;CSS樣式編寫:用CSS控制頁面外觀和佈局;JavaScript交互實(shí)現(xiàn):編寫代碼實(shí)現(xiàn)頁面動(dòng)效和交互;性能優(yōu)化:壓縮圖片、代碼和減少HTTP請求,提升頁面加載速度。

Angular應(yīng)用中:如何通過鼠標(biāo)懸停改變圖標(biāo)顏色? Angular應(yīng)用中:如何通過鼠標(biāo)懸停改變圖標(biāo)顏色? Apr 05, 2025 pm 02:15 PM

在Angular應(yīng)用中,如何實(shí)現(xiàn)鼠標(biāo)懸停在圖標(biāo)上時(shí)改變圖標(biāo)的顏色?許多開發(fā)者在使用Angular構(gòu)建應(yīng)用時(shí),會遇到需?...

為什麼Edge瀏覽器中的特定div元素?zé)o法顯示?如何解決這個(gè)問題? 為什麼Edge瀏覽器中的特定div元素?zé)o法顯示?如何解決這個(gè)問題? Apr 05, 2025 pm 08:21 PM

如何解決用戶代理樣式表導(dǎo)致的顯示問題?在使用Edge瀏覽器時(shí),項(xiàng)目中的一個(gè)div元素?zé)o法顯示。經(jīng)過查看,發(fā)?...

動(dòng)態(tài)網(wǎng)頁元素XPath和Class名變化頻繁,如何穩(wěn)定抓取目標(biāo)a標(biāo)籤? 動(dòng)態(tài)網(wǎng)頁元素XPath和Class名變化頻繁,如何穩(wěn)定抓取目標(biāo)a標(biāo)籤? Apr 01, 2025 pm 04:12 PM

動(dòng)態(tài)網(wǎng)頁元素抓取難題:應(yīng)對XPath和Class名變化很多爬蟲開發(fā)者在抓取動(dòng)態(tài)網(wǎng)頁時(shí)會遇到一個(gè)棘手的問題:目標(biāo)?...

css選擇器優(yōu)先權(quán)是什麼 css選擇器優(yōu)先權(quán)是什麼 Apr 25, 2024 pm 05:30 PM

CSS 選擇器優(yōu)先權(quán)依下列順序決定:特殊性(ID > 類別> 類型> 通配符)來源順序(行內(nèi)> 內(nèi)部樣式表> 外部樣式表> 使用者代理樣式表)宣告順序(靠後的宣告優(yōu)先)重要性(!important 強(qiáng)制提高優(yōu)先權(quán))

See all articles