html - Apply attr to div but not to children (JQuery) -


i have html code looking this:

<div id='main'>     <a href=''>     <div id='othera'>             <a href=''>     </div>     <div id='otherb'>             <a href=''>     </div> </div> 

i apply attribute "target" elements of #main not of #other using jquery. tried few things , think closest got not seem work.

$(document).ready(function(){     $('#main a').not(['#othera a', '#otherb a']).attr('target', '_blank'); }); 

use #main > a selector. pick 'a' tags under #main.

$(document).ready(function(){      $('#main > a').attr('target', '_blank');  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>  <div id='main'>      <a href=''>main a</a>      <div id='othera'>              <a href=''>asdsf</a>      </div>      <div id='otherb'>              <a href=''>kjklsdf</a>      </div>  </div>


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -