toString() not working
I am working with jQuery tabs and I've got some code that fires off when I change tabs.
$('#container-1').tabs({ onClick: function(clickedTab, divElement, hiddenTab) {
var selectedTab = clickedTab.toString();
// var pos = selectedTab.IndexOf("#") + 1;
var results = selectedTab.substring(5);
// selectedTab.IndexOf("#") + 1
alert(results);
}
});
I've commented out the offending code, but when I try to determine the position of the # character, I get an error:
Object http://www.omnicom-innovations.com/play/tabsdemo1.html#fragment-2 has no method 'IndexOf'
I was under the imporession that by using the toStri开发者_运维知识库ng() method, it would convert the object to a string. This is based off of my understanding of a similar post:
jQuery and split not working together?
If anyone can point out what's wrong, I'd greatly appreciate it.
indexOf starts with a lower case I.
As far as I understood you are simply trying to get the value after #. To perform string operations in JavaScript, preferably you should use Regular Expressions because JavaScript is really fast interpreting them.
For this example you could do the following:
$('#container-1').tabs({ onClick: function(clickedTab, divElement, hiddenTab) {
var selectedTab = clickedTab.toString();
//Matches all characters after a #
var results = /#.+/.exec(selectedTab);
//var results will contain all matches of the used Expression as an Array, so you want to get the first result
alert(results[0]);
}
});
I hope this helps.
精彩评论