与indexOf()匹配的多个字符串 [英] Multiple string matches with indexOf()

查看:194
本文介绍了与indexOf()匹配的多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定我的语法错了,因为脚本只有在字符串匹配Video时才起作用,如果字符串中有wordAudio,那么它会被忽略。重定向为../../../index.html不起作用。





<$ p $($)$ $ $ $ $ $ $ $ $ var $ = $ b $('a')。click(function(){
var srcTag = $(this).find('img')。attr('src');
if(srcTag.indexOf( 'Video'||'Audio')> -1){
if(confirm('Download Safari?\\\
\\\
http://apple.com/safari/download/')){
window.location ='http://apple.com/safari/download/';
} else {window.location ='../../../index.html';}
} else {
alert('no match');
}
});
}

html

 < a hre f =#>< 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>


解决方案

将此转换为正则表达式要短得多。

  if(srcTag.match(/(video | audio)/)){
/ * Found * /
}其他{
/ *未找到* /
}

请注意,请不要做你想做的事。要求用户在使用Internet Explorer 8时下载Safari会对互联网以及该用户造成伤害。



至于将域名重定向到另一个域名位置,您应该使用 .preventDefault()以使浏览器不会跟随链接:

 < (); code> $(a.videoDownload)。on(click,function(e){
e.preventDefault();
if(this.getElementsByTagName(img)[ 0] .src.match(/(video | audo)/)){
window.location = confirm('下载Safari?')
?http://apple.com/safari/download
:../../../index.html;
} else {
/ *不匹配* /
}
});

再次请不要 没有人希望成为那个人,并且当你告诉用户下载另一个浏览器时,你就是那个人


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.

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

<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>

解决方案

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.

As for redirecting the domain to another location, you should use .preventDefault() to keep the browser from following the link:

$("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 actually do this. Nobody wants to be that guy, and when you tell users to download another browser, you're being that guy.

这篇关于与indexOf()匹配的多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆