gpt4 book ai didi

javascript - 使用 jQuery 将 CSS 添加到带有 alt 标签的特定图像

转载 作者:行者123 更新时间:2023-12-01 08:30:17 25 4
gpt4 key购买 nike

我正在尝试在图像 alt 标记中搜索特定文本,并使用 jQuery 将 CSS 添加到该图像。我正在使用 jQuery 的 :contains ,但我不确定这是否是最好的方法?这是我到目前为止正在尝试的:

jQuery

var 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>

希望这是有道理的?

最佳答案

您不一定需要 javascript,只需使用 css 属性选择器即可创建选择器:https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

img[alt*='早上远足'] { border: 5pxsolid #000 }

我们可以在 jQuery 中使用相同的选择器

$("img[alt*='早上远足']").css("边框", "5px纯黑");

关于javascript - 使用 jQuery 将 CSS 添加到带有 alt 标签的特定图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61217284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com