修改A元素,但仅当它包含在特定范围 ID 内时
我有以下 jQuery 小技巧,可以使 Warning
中显示的任何 HTML 显示为大而红色:
$("strong:contains('Warning')").css("color","red").css("font-size","400%");
它工作得很漂亮。但是,它会使所有内容变大且呈红色,而且我真的只希望它使警告也变大并呈红色(如果它们也出现在 ID 为 myWarning
的跨度中)。例如:
<span id="myWarning">
<div align="left" class="tip">
<strong>Warning</strong> (I want this to be big and red...)
</div>
</span>
<strong>Warning</strong> (...but I don't want this to be big and red)
我怎样才能增强我的 jQuery hack 让它只修改第一个“警告”的 CSS 而不是第二个?
I have the following little jQuery hack to make any HTML that shows up in <strong>Warning</strong>
appear big and red:
$("strong:contains('Warning')").css("color","red").css("font-size","400%");
It works beautifully. However, it makes everything big and red, and I really only want it to make Warnings big and red if they also appear in a span with the id myWarning
. For example:
<span id="myWarning">
<div align="left" class="tip">
<strong>Warning</strong> (I want this to be big and red...)
</div>
</span>
<strong>Warning</strong> (...but I don't want this to be big and red)
How can I enhance my jQuery hack to get it to only modify the CSS of the first "Warning" and not the second?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需在选择器前面添加一个
#myWarning
即可?Just prepend a
#myWarning
to your selector?我想你希望第一个这样的警告是大的和红色的?
I think you want just the first such warning to be big and red?
你为什么使用 jQuery 来实现这个目的?为什么不简单地使用 CSS:
Why are you using jQuery for that? Why not simply use CSS: