获取包含属性onclick的image,然后隐藏img

get image contain the attribute onclick then hide the img

本文关键字:然后 隐藏 img image 包含 属性 onclick 获取      更新时间:2023-09-26

选择器可以获取包含属性onclick 的图像

onclick中有一个JS代码(windows.open….href以(/dailyTtickets/front/requesttype.form.php)开头)

我想要这样的东西:

 var attr = $('img').attr('onclick');
    if(typeof attr !== 'undefined' && attr !== false)
    {
$ImgIndex = $('img').attr('onclick').value().indexOf("var w = window.open('/dailyTickets/front/requesttype.form.php").index();
       if ($('img').attr('onclick').value().indexOf("var w = window.open('/dailyTickets/front/requesttype.form.php") == 0)
    {
       $('img:eq('"+$ImgIndex +"').hide();
    }
    }

您可以在此处进行测试:http://jsfiddle.net/zu44E/3/

这是图片:

<img 
    alt="" 
    title="Ajouter" 
    src="/dailyTickets/pics/add_dropdown.png" 
    style="cursor:pointer; margin-left:2px;" 
    onclick="var w = window.open('/dailyTickets/front/requesttype.form.php?popup=1&amp;rand=1489777051', 'glpipopup', 'height=400, width=1000, top=100, left=100, scrollbars=yes' );w.focus();"
></img>

我认为您的方法有点错误。首先。这是我的更新:

http://jsfiddle.net/zu44E/7/

以下是我所做的主要更改:

  1. 选择一个attr后,调用value()不会得到该属性的值
  2. 如果将一个变量分配给一系列链式函数调用,那么最终得到的是链中最后一个值,而不是第一个值。因此,如果方法有效,$ImgContains变量的值将为true/false,因为您将indexOf等同于0(因此是布尔值、true或false)
  3. 利用jquery。Jquery可以对记录执行批量操作。你不需要一个元素一个元素地寻找东西。如果您需要进行更深入的测试,并且测试或多或少是逐元素的,请使用迭代jquery方法,如filter

你想要这个吗?这将获得属性为onclick 的所有图像

$('img[onclick]')

隐藏具有该属性的所有图像

$('img[onclick]').hide()