我可以识别具有特定href

时间:2015-06-09 11:57:46

标签: javascript jquery html

我正在开发一个Web应用程序,我需要调整人员选择器对话框高度。目前在用户打开/关闭对话框时继续触发脚本,我设置了一个计时器(脚本运行2秒),如下所示: -

var interval = null; //Defines the start interval variable

$(document).ready(function () { // jQuery needed for this
    /* People Picker Fix Starts */
    if (navigator.appVersion.indexOf("MSIE 10") > -1) { // IE 10 Specific condition for People Picker Bug
       interval = setInterval(adjustPeoplePicker, 2000);
    }
    /* People Picker Fix Ends */
});

function adjustPeoplePicker() {
    if ($('.ms-dlgFrame').contents().find('#resultcontent').length > 0) {
        $('.ms-dlgFrame').contents().find('#resultcontent').css('height', '350px');
        $('.ms-dlgFrame').contents().find('#MetadataTreeControlTreeSearch').css('height', '350px');
       //clearInterval(interval);
    }
}

这是打开对话框的实际标记: -

<a id="ctl00_ctl41_g_a4fb58d0_ad0d_40cf_a4a3_ccabea410e43_ff141_ctl00_ctl00_UserField_browse" href="javascript:" onclick="__Dialog__ctl00_ctl41_g_a4fb58d0_ad0d_40cf_a4a3_ccabea410e43_ff141_ctl00_ctl00_UserField(); return false;" title="Browse">
<img alt="Browse" src="/_layouts/15/images/addressbook.gif" title="Browse">
</a>

所以我的问题是,只有当用户点击其中<a>内有<imag>的{​​{1}}时,我才可以触发该脚本,而不是每隔2次触发脚本秒??

3 个答案:

答案 0 :(得分:2)

$('a img').on('click', callScriptForImage);

....
// it is executed every time, but will call function adjustPeoplePicker()
// only if src attribute includes addressbook.gif, as you asked
function callScriptForImage(e){
  var src = $(this).attr('src');
  // read image src attribute
  if( /addressbook\.gif/.test(src)){
    // if src attribute includes addressbook.gif, call function
    adjustPeoplePicker();
  }
}

您还可以通过以下方式收听具有src属性的图片的点击次数:

$('a img[src$="addressbook.gif"]').on('click', adjustPeoplePicker);

这种方式更加清洁。

答案 1 :(得分:1)

我假设您要检查<img> src = addressbook.gif,而不是href <a> }?

如果是这种情况,这应该适合你:

$('a img').on('click', function () {
    //Check to see if if the href matches addressbook.gif
    var src = $(this).attr('src');
    var regex = /addressbook\.gif/i;
    if (regex.test(src)) {
        // Execute your code here.
    } else {
        //Put anything else you want here
    }
});

希望这有帮助!

JRad The Bad

答案 2 :(得分:1)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tbody id="search_body">
</tbody>
  </table>

https://jsfiddle.net/7u3m2cng/1/