The book I am reading tells me to open up the JavaScript console and try the code "foo: bar".indexOf(":"). I've tried it in many ways. I tried removing quotation marks, putting it inside a show() and alert() function. I just can't seem to tease anything out.

Has something changed in JavaScript? Has the author made a mistake? Am I supposed to get no return? Do I need to append document.write, perhaps? Any help greatly appreciated.

有帮助吗?

解决方案

Yes something changed in Firefox 5+ However the console (ctrl-shift-k) still works

Fx konsole

In the error console (ctrl-shift-J) you will need to wrap it in alert:

enter image description here

其他提示

foo:bar is a property definition in json, and indexOf is supposed to deal with a left value (string variable, constant, or at least something that can have characters in it. I don't know why the book you are reading wants you to do this, but it doesn't seem to be correct. The correct way to use indexOf would be :

var myObject = {
   foo:"bar"
}

alert(myObject.foo.indexOf("a"));

try like follows, it should work. Generally the indexOf() will return -1 if the value to search for never occurs.

var str="foo:bar";
document.write(str.indexOf(":") + "<br />");

The output should be 3

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top