代码片段 → jQuery 代码片段 → 自动发现文档链接并应用类 自动发现文档链接并应用类 Chris Coyier 于 2009年8月10日 $('a[href]').each(function() { if((C = $(this).attr('href').match(/[.](doc|xls|pdf)$/))) { $(this).addClass(C[1]); } }); 这将遍历页面上的每个a元素。如果它的 href 属性包含 .doc、.xls 或 .pdf,它将应用相应的类名(例如class="doc")
完美运行!
$(“a[href$=’.doc’]”).addClass(‘doc’);
虽然 IE6 会降级为普通链接,但这可以通过 CSS 来实现。
a[href$='.doc'] { padding-right:16px; background: transparent url(images/doc.png) no-repeat top right; }
是的,CSS 方法比 jQuery 方法更好(个人观点):)。
太棒了,谢谢