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

时间:2016-01-04 23:19:47

标签: javascript jquery-selectors src attr

如何检查以下src是否包含“green”文本

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

2 个答案:

答案 0 :(得分:1)

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

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

   console.log('success') 

或者只是检查属性

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

   console.log('success') 

答案 1 :(得分:1)

尝试indexOf

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

希望这有帮助。