Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

javascript - 检查字符串是否以某事开头? [重复](Check if string begins with something? [duplicate])

Possible Duplicate:(可能重复:)

I know that I can do like ^= to see if an id starts with something, and I tried using that for this, but it didn't work... Basically, I'm retrieving the url and I want to set a class for an element for pathnames that start in a certain way...(我知道我可以这样做^ =看看id是否以某个东西开头,我尝试使用它,但它没有用......基本上,我正在检索网址,我想要设置一个类对于以某种方式开始的路径名的元素...)

So,(所以,) var pathname = window.location.pathname; //gives me /sub/1/train/yonks/459087 I want to make sure that for every path that starts with /sub/1, I can set a class for an element...(我想确保对于以/ sub / 1开头的每个路径,我可以为元素设置一个类......) if(pathname ^= '/sub/1') { //this didn't work... ...   ask by n00b0101 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use stringObject.substring(使用stringObject.substring)

if (pathname.substring(0, 6) == "/sub/1") { // ... }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...