简体   繁体   中英

access anchor tag with innerhtml

I have system generated anchor tag and it doesn't contain id or class. Only thing I know that it has fixed inner html(like "Clear"). Can we access this anchor tag with inner html. So on click of this I can hide another div tag.

 .wrap-div{ width:70px; height:100px; background-color:black; } #submit{ display:block; position: relative; } 
 <div class='wrap-div' ></div> <div id="submit"> <a onclick="fun1();" href="javascript:{}"> Apply </a> | <a onclick="fun2()" href="javascript:{}"> Clear </a> </div> 

You can use attribute equals selector

$("a[onclick='fun2()']")

 $("a[onclick='fun2()']").css("color", "red"); 
 .wrap-div { width: 70px; height: 100px; background-color: black; } #submit { display: block; position: relative; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='wrap-div'></div> <div id="submit"> <a onclick="fun1();" href="javascript:{}"> Apply </a> | <a onclick="fun2()" href="javascript:{}"> Clear </a> </div> 

or :contains()

$("a:contains('Clear')")

 $("a:contains('Clear')").css("color", "sienna"); 
 .wrap-div { width: 70px; height: 100px; background-color: black; } #submit { display: block; position: relative; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='wrap-div'></div> <div id="submit"> <a onclick="fun1();" href="javascript:{}"> Apply </a> | <a onclick="fun2()" href="javascript:{}"> Clear </a> </div> 

Try this code in jQuery:

$('#submit a').each(function(i, item) {
  if($(item).text() == 'Clear')
  {
      // here you have access to that a tag
      console.log(item);
      return;
  }
})​;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM