小编And*_*tis的帖子

如何检查 URL 是否包含数字?

我正在使用 window.location.href.indexOf 来检查 URL 是否包含一个字符串并且非常适合这样的事情:

if (window.location.href.indexOf("franky") > -1) {
      alert("your url contains the name franky");
Run Code Online (Sandbox Code Playgroud)

但是检查 URL 是否包含任何数字不起作用。即使 URL 中没有数字,以下内容也始终会调用警报。

if (
window.location.href.indexOf("0") === -1 || 
window.location.href.indexOf("1") === -1 || 
window.location.href.indexOf("2") === -1 || 
window.location.href.indexOf("3") === -1 || 
window.location.href.indexOf("4") === -1 || 
window.location.href.indexOf("5") === -1 || 
window.location.href.indexOf("6") === -1 || 
window.location.href.indexOf("7") === -1 || 
window.location.href.indexOf("8") === -1 || 
window.location.href.indexOf("9") === -1
)
{ alert("false"); }
Run Code Online (Sandbox Code Playgroud)

javascript url jquery

0
推荐指数
1
解决办法
264
查看次数

标签 统计

javascript ×1

jquery ×1

url ×1