Jquery单击隐藏元素

时间:2017-02-05 14:07:36

标签: javascript jquery html events

我有

<table width="60" height="60" cellpadding="0" 
cellspacing="0" border="0" style="float: left; margin: 1px" 
background="images/data/source_red.gif">
   <tbody>
      <tr>
         <td act1="7" act3="8" store="true" art_id="4949" cnt="1" div_id="AA_4949" 
         onmouseover="artifactAlt(this,event,2)" 
         onmouseout="artifactAlt(this,event,0)" 
         valign="bottom" 
         style="background-image: url(&quot;images/d.gif&quot;); cursor: pointer;">&nbsp;</td>
      </tr>
   </tbody>
</table>

我想点击onmouseover="artifactAlt(this,event,2)"触发时上升的元素,该怎么做?

我正在做$('#body').contents().find('td[art_id="4949"]')[0].click();

我得到undefined并且没有发生。

2 个答案:

答案 0 :(得分:5)

您应该使用.click()方法。

function artifactAlt(obj,event,number){
     $(obj).click();
}

function artifactAlt(obj,event,number){
   $(obj).click();
}

$('tr').click(function(){
  alert('tr clicked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="60" height="60" cellpadding="0" 
cellspacing="0" border="0" style="float: left; margin: 1px" 
background="images/data/source_red.gif">
   <tbody>
      <tr>
         <td act1="7" act3="8" store="true" art_id="4949" cnt="1" div_id="AA_4949" 
         onmouseover="artifactAlt(this,event,2)" 
         onmouseout="artifactAlt(this,event,0)" 
         valign="bottom" 
         style="background-image: url(&quot;images/d.gif&quot;); cursor: pointer;">abcd</td>
      </tr>
   </tbody>
</table>

答案 1 :(得分:0)

您应该尝试jquery trigger,如下所示

function artifactAlt(element, event, number) {
  $(element).trigger('click');
}

  

使用trigger('click')将为您节省一个函数调用,因为jQuery在内部调用了this post中指出的$()。click()函数调用的触发器。