jquery通过rel标签查找锚点
我有一个看起来像这样的锚标记,
<a rel="C:\dev/ACME.Console/3rdPartyBinaries/" href="#">3rdPartyBinaries</a>
它可能嵌套在 li 或 ul 标记中
,我试图在单击时设置这些锚标记的背景颜色。我现在正在使用以下功能来做到这一点。
function (file) {
$('a[rel="' + file + '"]').css("background", "#eee");
因此,当单击锚元素时,就会触发上述函数,并且文件参数将作为锚 rel 属性传递。但是用上面的方法并没有设置背景。
任何帮助将不胜感激
I have an anchor tags which look like this
<a rel="C:\dev/ACME.Console/3rdPartyBinaries/" href="#">3rdPartyBinaries</a>
which may be nested in li or ul tags
and I am trying to set the background color of those anchor tags when clicked. I am right now doing that with the following function.
function (file) {
$('a[rel="' + file + '"]').css("background", "#eee");
So when ever an anchor element is clicked the above function is hit and the file parameter is passed as the anchors rel attribute. But the background is not being set with the above method.
Any help would be greatly appreaciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要对字符串中的
\
进行双重转义。目前,您的
\
正在转义d
,这不是一个特殊的转义序列,因此只是返回d
。您需要转义转义字符,然后再次转义选择器字符串。jsFiddle。
您可以在函数内部处理选择器的转义序列,而不是记住对其进行两次转义。
jsFiddle。
You need to double escape the
\
in your string.At the moment, your
\
is escaping thed
, which isn't a special escape sequence so is just returningd
. You need to escape the escape character and then escape it again for the selector string.jsFiddle.
Instead of remembering to escape it twice, you could handle the escape sequence for the selector inside of the function.
jsFiddle.