I have some markup that looks like this:
<div id="breadcrumb"><a href="http://domain.com/site/">Site</a> »
<a href="http://domain.com/site/sub/"></a> »
<a href="http://domain.com/site/sub/sub/">Heading</a> » Document title</div>
On the page, it looks like:
Site » » Heading » Document title
I'd like to use jQuery to look for the empty "a" tag, remove it, and then remove one of the >> characters.
I have this:
$(document).ready(function() {
if ($('#breadcrumb').length) {
$('#breadcrumb a:empty').replaceWith('-');
$('#breadcrumb').html($('#breadcrumb').html().replace(/» - »/, "»"));
}
});
Notice that the >> characters are开发者_开发百科 in the Javascript code itself. This works, but it doesn't seem right. I am looking for the proper way to find that character via regex. Or, I am looking for someone to say that this code is fine. :) I've tested it, and it seems to work in the browsers I've used. Thanks.
(I will eventually fix the process that creates these breadcrumbs, so that it no longer outputs the empty tag... but I am looking for a stop-gap!)
You're good, regex expressions in javascript are denoted using slashes in the same way that string are denoted by quotes. It's confusing when you first see it but this is a perfectly valid javascript regex expression.
I would use jQuery DOM manipulation API
next()
and
remove()
$('#breadcrumb a:empty').next().remove();
$('#breadcrumb a:empty').remove();
精彩评论