jQuery uses CSS selectors. a[^="https://stackoverflow.com/"]
will select all <a>
whose href
attribute starts with /
which are children of whatever the this
is.
See it in action:
$("ul").each(function () {
$(this).find("a[href^="https://stackoverflow.com/"]").addClass("selected");
});
.selected {
background-color: lime;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li><a href="http://www.google.com">Will not be selected</a></li>
<li><a href="http://stackoverflow.com/example">Will be selected</a></li>
</ul>
<ul>
<li><a href="http://stackoverflow.com/example">Yep</a></li>
<li><a href="http://www.google.com">Nope</a></li>
</ul>
- jQuery documentation on starts with attribute selector: https://api.jquery.com/attribute-starts-with-selector/
- More on attribute selectors: https://developer.mozilla.org/en/docs/Web/CSS/Attribute_selectors
1
solved What does this line of Jquery do?