【问题标题】:JS. check if input attribute src contains textJS。检查输入属性 src 是否包含文本
【发布时间】:2016-01-04 23:19:46
【问题描述】:

如何检查下面的 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') 

【问题讨论】:

    标签: javascript jquery-selectors src attr


    【解决方案1】:

    试试indexOf

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

    希望这会有所帮助。

    【讨论】:

    • 我之前尝试过那个 indexof,但我必须编码不正确。您的示例有效
    【解决方案2】:

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

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

    或者只检查属性

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

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2021-10-30
      • 2013-03-04
      • 1970-01-01
      • 2021-08-18
      相关资源
      最近更新 更多