在多个 url 字符串上使用 .indexOf
Using .indexOf on more than one url string
我有一个函数,当页面的 URL 以 /about.html
结尾时运行
window.onload = function() {
if (window.location.href.indexOf('/about.html') > -1) {
$("#logo-black").typed({
strings: ["Nothing^450&Co^250.^500", "^800__^400&Co^600."],
typeSpeed: 70,
backSpeed: 100,
callback: function() {
$(".typed-cursor").css("display", "none"),
setTimeout(function(){
$('.main-load').toggleClass('main-load-active'),
$('.nav-text').toggleClass('nav-text-active');
},400),
$('.nav-reveal').toggleClass('nav-reveal-active');
}
});
}
}
但我想将此功能应用到其他页面(contact.html 等)。
如何让 .indexOf 处理多个字符串?
如有help/advice/suggestions/constructive批评,不胜感激!
我知道这是微不足道的,但我一直不确定哪种解决方案在这里最有效。
您可以使用.test()
console.log(window.location.href)
if (/(about.html|contact.html|js)/.test(window.location.href)) {
console.log("Contains either: about.html OR contact.html OR js")
} else {
console.log("Can't find either: about.html OR contact.html OR js")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我有一个函数,当页面的 URL 以 /about.html
结尾时运行window.onload = function() {
if (window.location.href.indexOf('/about.html') > -1) {
$("#logo-black").typed({
strings: ["Nothing^450&Co^250.^500", "^800__^400&Co^600."],
typeSpeed: 70,
backSpeed: 100,
callback: function() {
$(".typed-cursor").css("display", "none"),
setTimeout(function(){
$('.main-load').toggleClass('main-load-active'),
$('.nav-text').toggleClass('nav-text-active');
},400),
$('.nav-reveal').toggleClass('nav-reveal-active');
}
});
}
}
但我想将此功能应用到其他页面(contact.html 等)。
如何让 .indexOf 处理多个字符串?
如有help/advice/suggestions/constructive批评,不胜感激! 我知道这是微不足道的,但我一直不确定哪种解决方案在这里最有效。
您可以使用.test()
console.log(window.location.href)
if (/(about.html|contact.html|js)/.test(window.location.href)) {
console.log("Contains either: about.html OR contact.html OR js")
} else {
console.log("Can't find either: about.html OR contact.html OR js")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>