JS.检查输入属性src是否包含文本



我如何检查下面的src是否包含文本"绿色"

 <input onclick="myfunction('' + 'match' + '')" id="btn_match" type="image" src="http://localhost/content/tick_green.png" />

例如,这是我得到的最接近的

if($('#btn_match [src*="green"]')==true ) console.log('success') 

尝试indexOf:

if( $('#btn_match').attr('src').indexOf('green') != -1 ) 
     console.log('success');

希望这能有所帮助。

您必须检查jQuery选择的长度,看看是否有任何元素与选择器匹配

if ( $('#btn_match[src*="green"]').length > 0 ) 
   console.log('success') 

或者只检查属性

if ( $('#btn_match').attr('src').indexOf('green') !== -1 )
   console.log('success') 

最新更新