jsイベントをクリックして現在の要素または親要素を取得

9488 ワード

1.ラベルを設定することでjsにclickイベントを書く


html:

<div style="margin-left: 77px; background-color: white; line-height: 30px; padding-left: 5px;">
     <a class="pro deleteBrand" title="    " href="javascript:void(0);">    
        <span class="icon-btn-x">xspan>
     a>
     <a class="pro deleteBrand" title="    " href="javascript:void(0);">    
         <span class="icon-btn-x">xspan>
     a>
     <a class="pro deleteBrand" title="    " href="javascript:void(0);">    
         <span class="icon-btn-x">xspan>
     a>
     <a class="pro" id="btn-brand-search" title="    " href="javascript:void(0);">a>
  div>

js:

deleteBrand : function () {
  $(".deleteBrand").click(function (aa) {
     $(this).css('display', 'none');
   //$(this).parent().css('display', 'none');
  });
}

2.htmlページにclickイベントを書く


html:

<div style="margin-left: 77px; background-color: white; line-height: 30px; padding-left: 5px;">
   <a class="pro" data-url="nav"  title="    " href="javascript:void(0);" onclick="new goodsUpperAndLower.init().deleteBrand(this);">    
       <span class="icon-btn-x">xspan>
   a>
   <a class="pro" data-url="nav"  title="    " href="javascript:void(0);" onclick="new goodsUpperAndLower.init().deleteBrand(this);">    
       <span class="icon-btn-x">xspan>
   a>
   <a class="pro" data-url="nav"  title="    " href="javascript:void(0);" onclick="new goodsUpperAndLower.init().deleteBrand(this);">    
       <span class="icon-btn-x">xspan>
   a>
   <a class="pro" id="btn-brand-search" title="    " href="javascript:void(0);">a>
 div>

js:

deleteBrand : function (obj) {
   //$(obj).parent().css('display', 'none');
   $(obj).css('display', 'none');
}

 
転載先:https://www.cnblogs.com/jcjssl/p/9546370.html