Javascript 多个字符串与 indexOf() 匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10537294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple string matches with indexOf()
提问by Blainer
Im pretty sure my syntax this wrong because the script only works if the string matches "Video", if the string has the "word "Audio" it is ignored. Also since the href tags have a value of "#" the redirect for "../../../index.html" doesnt work.
我很确定我的语法是错误的,因为该脚本仅在字符串匹配“视频”时才有效,如果字符串具有“音频”一词,则它被忽略。此外,由于 href 标签的值为“#”,因此重定向为“ ../../../index.html”不起作用。
js
js
var ua = navigator.userAgent.toLowerCase();
var isIE8 = /MSIE 8.0/i.test(ua);
if (isIE8) {
$('a').click(function () {
var srcTag = $(this).find('img').attr('src');
if (srcTag.indexOf('Video' || 'Audio') > -1) {
if (confirm('Download Safari? \n\n http://apple.com/safari/download/')) {
window.location = 'http://apple.com/safari/download/';
} else { window.location = '../../../index.html';}
} else {
alert('no match');
}
});
}
html
html
<a href="#"><img src="Video/000_Movies/assets/005_CCC_Jesus_Story_80x60.jpg" />test1</a>
<a href="#"><img src="Audio/000_Movies/assets/006_GSP_Gods_Story_80x60.jpg" />test2</a>
<a href="#"><img src="Media/000_Movies/assets/002_God_Man_80x60.jpg" />test3</a>
回答by Sampson
It's far shorter to turn this into a regular expression.
将其转换为正则表达式要短得多。
if ( srcTag.match( /(video|audio)/ ) ) {
/* Found */
} else {
/* Not Found */
}
On a side note, please don't do what you're attempting to do. Asking users to download Safari when they're using Internet Explorer 8 is doing a disservice to the Internet, as well as to that user.
附带说明一下,请不要做您正在尝试做的事情。要求用户在使用 Internet Explorer 8 时下载 Safari 是对 Internet 以及该用户的损害。
As for redirecting the domain to another location, you should use .preventDefault()
to keep the browser from following the link:
至于将域重定向到另一个位置,您应该使用.preventDefault()
以防止浏览器跟随链接:
$("a.videoDownload").on("click", function(e){
e.preventDefault();
if ( this.getElementsByTagName("img")[0].src.match( /(video|audo)/ ) ) {
window.location = confirm( 'Download Safari?' )
? "http://apple.com/safari/download"
: "../../../index.html" ;
} else {
/* No match */
}
});
Again, please don't actuallydo this. Nobody wants to be that guy, and when you tell users to download another browser, you're being that guy.
同样,请不要真的这样做。没有人想成为那个人,当你告诉用户下载另一个浏览器时,你就是那个人。
回答by David M
'Video' || 'Audio'
is a logical OR. A non-empty string is implicitly a true value in JavaScript, and thus the short-circuited OR is not evaluated and this collapses to just 'Video'
. This is why you see the results you do.
'Video' || 'Audio'
是逻辑 OR。非空字符串在 JavaScript 中隐式为真值,因此不会评估短路的 OR 并且这会折叠为仅'Video'
. 这就是为什么你会看到你所做的结果。
Others have pointed you in correct directions to resolve.
其他人已为您指明了解决问题的正确方向。
回答by howard10
I think you probably want the OR (||) operator outside indexOf as so:
我认为您可能希望在 indexOf 之外使用 OR (||) 运算符,如下所示:
if ((srcTag.indexOf('Video') !== -1) || (srcTag.indexOf('Audio') !== -1)) {
...
}
回答by Selvakumar Arumugam
I think what you need is 2 separate indexOf like below,
我认为你需要的是 2 个单独的 indexOf,如下所示,
srcTag.indexOf('Video') != -1 || srcTag.indexOf('Audio') != -1
回答by bygrace
Yeah, you would need to something like this do this:
是的,你需要做这样的事情:
if (srcTag.indexOf('Video') > -1 || srcTag.indexOf('Audio') > -1) {
回答by Mohan Dere
回答by Somnath Kharat
This will also work:
这也将起作用:
if (srcTag.indexOf('Video') >= -1 || srcTag.indexOf('Audio') >=-1 ) {
回答by majo manoj
This Worked for me:
这对我有用:
if (srcTag.indexOf('Video' | 'Audio' ) >= -1 ) {