繁体   English   中英

检查以查看子div在父DIV中具有特定链接

[英]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>

我如何使用jQuery / Javacript#posts表的 #name div中查找带有“ abc ”的链接。

并找到它之后(带有abc的链接)。 jQuery会使用html();将html内容添加到该表的 #post-content #load div中,该表的内部带有“ abc”链接 jQuery中的功能。

这意味着添加我的代码后将如下所示:

 <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>

而没有与“ abc”链接(即与“ user2”链接)的#name 不会受到影响或修改。

我怎样才能做到这一点?

我希望你明白。

  1. 查找带有href且包含“ abc”的标签
  2. 获取父母tr
  3. 在此元素中找到#postcontent >#load

     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()');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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