我正在嘗試在圖像 alt 標記中搜索特定文本,并使用 jQuery 將 CSS 添加到該圖像。我正在使用:containsjQuery,但我不確定這是否是最好的方法?到目前為止,這就是我正在嘗試的:jQueryvar imgAlt = $("img").attr("alt");$("imgAlt:contains('morning hike')").css("border", "5px solid black");HTML:<div> <img src="https://images.unsplash.com/photo-1584879788725-56a4b394de8d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="candles on table"> <img src="https://images.unsplash.com/photo-1584354012731-ba34cef5963f?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="morning hike"></div>希望這是有道理的嗎?
1 回答

四季花海
TA貢獻1811條經驗 獲得超5個贊
您不一定需要 javascript,可以僅使用 css 屬性選擇器創建選擇器: https: //developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors
img[alt*='morning hike'] { border: 5px solid #000 }
我們可以在 jQuery 中使用相同的選擇器
$("img[alt*='morning hike']").css("border", "5px solid black");
- 1 回答
- 0 關注
- 140 瀏覽
添加回答
舉報
0/150
提交
取消