Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果单击,则img src包含特定文本_Javascript_Jquery - Fatal编程技术网

Javascript 如果单击,则img src包含特定文本

Javascript 如果单击,则img src包含特定文本,javascript,jquery,Javascript,Jquery,大家好,我怎样才能检查点击的img src是否包含特定的文本,如果然后做一些功能 例如: , 如果单击的img src包含类似“dog”的内容,请运行警报?试试这个 $('img').click(function(){ if($(this).attr('src').indexOf('img4-dog') >= 0) alert('found'); }); $('img').click(function(){ if($(this).attr('src').toLowerCa

大家好,我怎样才能检查点击的img src是否包含特定的文本,如果然后做一些功能

例如:

如果单击的img src包含类似“dog”的内容,请运行警报?

试试这个

$('img').click(function(){
    if($(this).attr('src').indexOf('img4-dog') >= 0) alert('found');
});
$('img').click(function(){
   if($(this).attr('src').toLowerCase().indexOf("dog") >= 0){
       alert('text found');
     }
});
$('img').click(function(){
   if (this.src.indexOf("dog") != -1) 
      alert('contains dog');
})
$('img').click(function(){
   if($(this).attr('src').toLowerCase().indexOf("dog") >= 0){
       alert('text found');
     }
});
$('img').on('click', function() {
    var imgsrc = $(this).attr('src');
   // Use a regular expression to find a match
    var test = imgsrc.search(/dog/);
    if (test > -1) {
      // do something
    }
});