简体   繁体   中英

check to see a child div has certain link in a parent DIV

<div id="posts">
        <table>
         <tr>
            <td>
              <div id="name" class="enabled"><a href="123.com/abc">User1</a></div>
            </td>
            <td>
              <div id="post-content">
                  Hello
              <div id="load">
              </div>
              </div>
            </td>
         </tr>
        </table>
    <br><br>
            <table>
             <tr>
                <td>
                  <div id="name" class="enabled"><a href="123.com/user2">User2</a></div>
                </td>
                <td>
                  <div id="post-content">
                      Hello too
                  <div id="load"></div>
                  </div>
                </td>
             </tr>
            </table>
         </div>

How can i use jQuery/Javacript to find link with " abc " inside #name div of #posts table .

and after locating it (the link with abc). jQuery will add html content to #post-content #load div of that table that has link with "abc" inside ,using the html(); function in jQuery.

that means after adding my code will look like this :

 <div id="posts">
    <table>
     <tr>
        <td>
          <div id="name" class="enabled"><a href="123.com/abc">User1</a></div>
        </td>
        <td>
          <div id="post-content">
              Hello
          <div id="load">
            Some stuff is added here using html()
          </div>
          </div>
        </td>
     </tr>
    </table>
<br><br>
        <table>
         <tr>
            <td>
              <div id="name" class="enabled"><a href="123.com/user2">User2</a></div>
            </td>
            <td>
              <div id="post-content">
                  Hello too
              <div id="load"></div>
              </div>
            </td>
         </tr>
        </table>
     </div>

while the #name with no link with "abc" which is the link with "user2" will not be affected or modifeid.

How can i do this?

I hope you understand.

  1. Find a tag with href containing 'abc'
  2. Get parent tr
  3. Find #postcontent >#load within this element

     var x = $('a[href*="abc"]'); var b = x.parents('tr'); var c = b.find('#post-content > #load'); alert(c.html()); 

假设您修复了html并使用了类:

$('.name a[href*="abc"]').closest('tr').find('.load').html('Some stuff is added here using html()');

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