更改 TD 上的 bgcolor,其中包含 a 和包含 XXX 的 onClick
我需要一个 jQuery 脚本,如果它包含包含“BG5”的 onClick,则可以更改所有 td 上的颜色。 它需要在 IE7 上运行。我已经尝试过 $("a:contains('BG5')").css("background","#F6CED8");
这可能吗?
他们通过按按钮来获得效果
<tr bgcolor='#F2F2F2'>
<td>2011-04-18</td>
<td>a</td>
<td>a</td>
<td align="center"><a href="#" onClick="edit('BG5')"><img src="../img/icon/skruenogle.png" border=0 alt="Delete corr"></A>
<td align="center"><a href="#" onClick="disregard('BG5')"><img class="click" src="../img/icon/redcross.png" border=0 alt="Delete corr"></A></td>
</tr>
<tr bgcolor='#FFFFFF'>
<td>2011-04-18</td>
<td>a</td>
<td>a</td>
<td align="center"><a href="#" onClick="edit('1A1')"><img src="../img/icon/skruenogle.png" border=0 alt="Delete corr"></A>
<td align="center"><a href="#" onClick="disregard('1A1')"><img class="click" src="../img/icon/redcross.png" border=0 alt="Delete corr"></A></td>
</tr>
I need a jQuery script that can change color on all td if it contain a onClick that contain "BG5".
It need to work on IE7. I have tried $("a:contains('BG5')").css("background","#F6CED8");
Is this possible?
They TD get the effect by a push on buttom
<tr bgcolor='#F2F2F2'>
<td>2011-04-18</td>
<td>a</td>
<td>a</td>
<td align="center"><a href="#" onClick="edit('BG5')"><img src="../img/icon/skruenogle.png" border=0 alt="Delete corr"></A>
<td align="center"><a href="#" onClick="disregard('BG5')"><img class="click" src="../img/icon/redcross.png" border=0 alt="Delete corr"></A></td>
</tr>
<tr bgcolor='#FFFFFF'>
<td>2011-04-18</td>
<td>a</td>
<td>a</td>
<td align="center"><a href="#" onClick="edit('1A1')"><img src="../img/icon/skruenogle.png" border=0 alt="Delete corr"></A>
<td align="center"><a href="#" onClick="disregard('1A1')"><img class="click" src="../img/icon/redcross.png" border=0 alt="Delete corr"></A></td>
</tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您要做的就是更改包含字符串“BG5”的
a
标记的每个tr
元素的背景颜色在名为onClick
的属性中。如果这是正确的,请尝试如下操作:
这是一个使用 HTML 和上述 jQuery 的 工作示例。基本上,它的作用是获取所有
a
元素,过滤它们,这样我们就只剩下那些在onClick
属性中包含字符串“BG5”的元素,获取最接近的 < code>tr 元素,并设置背景颜色。需要注意的重要一点是
attr
方法区分大小写,因此您需要确保与 HTML 一致!另外,您确实不应该使用
bgcolor
属性,因为它已被弃用。应始终使用 CSS。此外,您问题的代码中还有一些 td 元素尚未关闭。这可能只是问题中的拼写错误,但值得一提以防万一!If I've understood you correctly, what you're trying to do is change the background colour of every
tr
element that contains ana
tag which has the string "BG5" in an attribute namedonClick
.If that's along the right lines, try something like this:
Here's a working example using your HTML and the above jQuery. Basically what that does is get all the
a
elements, filter them so we are left with only those which contain the string "BG5" in theonClick
attribute, gets the closesttr
element to those, and sets the background colour.An important thing to note would be that the
attr
method is case sensitive, so you'll want to make sure you are consistent with your HTML!On a separate note, you really shouldn't be using the
bgcolor
attribute, as it's deprecated. CSS should always be used instead. Also, there's a couple oftd
elements which have not been closed in the code in your question. That may just be a typo in the question, but worth a mention just in case!