try substr(0)
first character is at index 0 http://www.w3schools.com/jsref/jsref_substr.asp
views:
204answers:
4I can't reproduce this. Sounds like an issue with the doSomething
code itself - do a quick code review to make sure you're pointing to the right function and that you're not modifying the argument before use.
$(document).ready(function(){
$("a.foo,a.bar").filter("[href^=#]").click(function() {
doSomething( $(this).attr("href").substr(1) );
return false;
});
doSomething = function(str) {
alert(str);
}
});
Works for me using
<a class="foo" href="#1111">1</a>
<a class="bar" href="#2222">2</a>
note: This isn't the correct answer! But I'm going to leave it here in case it is of use to other Sizzle users.
It's a bug in Sizzle (the selector library used by jQuery).
The problem is when you access link.href
in JavaScript you don't get the text of the href
attribute, you get the resolved absolute URL. Naturally this begins with http://
and not #
.
When you call link.attr('href')
jQuery detects the special case and applies a workaround. When you use a selector, Sizzle doesn't. It tries:
attrHandle: {
href: function(elem){
return elem.getAttribute("href");
}
},
but this workaround doesn't work on IE up to version 7, as they make getAttribute
equal to just getting the JavaScript property. [The correct workaround would be to use getAttributeNode
instead, or sniff IE and use the proprietary getAttribute('href', 2)
extension.]
So the selector engine gets http://...
for the href attribute and of course this never matches ^=#
.
If you do it yourself with a filter function it should work:
$('a.foo, a.bar').filter(function() {
return $(this).attr('href').substring(0, 1)==='#';
})...