Javascript lastIndexOf not working
Javascript lastIndexOf not working
我正试图使用纯javascript中的lastindexof函数从URL中删除fakepath但没有显示任何输出——以下是我的JS代码:-
function myFunction()
{
var str="C:'fakepath'somefile.txt";
var m=str.lastIndexOf("'");
var n=str.substring(m+1);
document.getElementById("demo").innerHTML=n;
}
以下是我的HTML代码:-
<p id="demo">
Click the button to locate where in the string a specified value occurs.
</p>
<button onclick="myFunction()">Try it</button>
您可以在我的jsfiddle中找到工作示例,网址为->http://jsfiddle.net/xKPaK/4/
- 必须转义
'
字符
反斜杠用于在字符串中创建特殊字符。例如,''n'
创建了一个值为换行符的字符串,而'''n'
创建了值为'n
的字符串 myFunction
必须在全局范围内才能在HTML属性回调中调用
这是jsfiddle的一个怪癖。JavaScript窗格的内容实际上是在一个函数中执行的,它为变量和函数创建了新的作用域。只需将函数添加到全局对象即可解决此问题
function myFunction() {
var str,
m,
n;
str = "C:''fakepath''somefile.txt";
m = str.lastIndexOf("''");
n = str.substring(m + 1);
document.getElementById("demo").innerHTML = n;
}
window.myFunction = myFunction;
小提琴
相关文章:
- $rootScope not working
- jQuery document.ready not working
- Javascript getElementsByTagName not working?
- JQuery.val( ) not working
- Tomcat websocket is not working
- Javascript JSON.parse not working
- NodeJS Multer is not working
- Regex not working
- Ember run.debounce not working
- AngularJs 1 not working
- document.getElementById.style.backgroundImage not working
- Rxjs5, distinct() is not working
- createOscillator noteOn not working
- Javascript - .getHours() not working
- Javascript setTimeout not working | onkeydown
- MongoDB $pull not working
- Express router.use not working
- JSONP not working
- window.opener not working in chrome & IE
- Backbone + RequireJS: Collection.each() is not working