我正在尝试编写一个jQuery选择器来查找包含在documents
文件夹中的文件的链接。网站上的各个地方都有几个文档文件夹。样本URL匹配是jQuery属性包含与包含斜线的文本的选择器
http://example.com/magazine/volume5/documents/magazine2010_11-8_final.pdf
这工作:
$("a[href*='documents']")
但为了完整,我想匹配两边的斜线。我看到要在选择器中包含斜杠,我需要在它前面加上两个反斜杠,如下所示:\\/
。但这只适用于最后的斜线,而不是主要的斜线。
这工作:
$("a[href*='documents\\/']")
但这并不:
$("a[href*='\\/documents\\/']")
我在做什么错?
更新 上的jsfiddle(http://jsfiddle.net/jA3AJ/),它的工作原理,但不是我的网站上(使用相同的浏览器,火狐4.0)。我完整的代码片段是
<script type="text/javascript">
// Instrument download links for GA
$(document).ready(function() {
$("a[href*='\\/documents\\/']").click(function() {
alert("Download: " + this.pathname);
// if (typeof _gaq !== 'undefined') { _gaq.push(["_trackEvent", "Files", "Download", this.pathname]); }
});
});
</script>
根据这个问题的答案How do I get jQuery to select elements with a . (period) in their ID?,我需要用两个反斜杠。 jsFiddle无论如何都适合我。事实上,它的工作原理没有反斜杠:$("a[href*='/documents/'])"
更新 我使用jQuery 1.6.4。我更新了jsFiddle:http://jsfiddle.net/jA3AJ/1/,那里,它工作正常。
两个版本都为我工作,既与当前报价设置和倒车它们如图API。 http://jsfiddle.net/Tentonaxe/ykvw5/ – 2012-01-13 21:04:18
是否将锚标签动态添加到页面中? – 2012-01-13 21:21:01
不,他们处于静态标记。 – 2012-01-13 21:26:23