如何在div中获取孙子的nodeName

时间:2018-06-06 07:50:32

标签: javascript jquery

我试图在任何(A标签)上触发点击事件以转到指定的HREF网址,它适用于父元素,但不适用于(cmpsocials)类下的大孩子。

请让我知道我哪里出错。

<li class="grid-item" style="cursor:pointer;">
  <div class="elisting-b bus-border">

    <div class="elisting-logo">
        <a class="userlink" href="https://website.com/listing/discountpi">
                                    <img width="307" height="104" src="https://website.com/wp-content/uploads/2018/06/discount-power-inc-logo.png" class="attachment-full size-full" alt="" srcset="https://website.com/wp-content/uploads/2018/06/discount-power-inc-logo.png 307w, https://website.com/wp-content/uploads/2018/06/discount-power-inc-logo-300x102.png 300w" sizes="(max-width: 307px) 100vw, 307px"></a>            </div>
    <div class="list-right">
        <div class="companyaddress">
            <h2 class="el-cname"><a class="userlink" href="https://website.com/listing/discountpi">Discount Power Inc.</a></h2>
            <div class="cphone">
                <div class="phn-icon">
                    <a href="tel:(203) 929-3200">(203) 929-3200</a>
                </div>
            </div>
            <div class="acdd">6 Armstrong Road, Shelton, NJ 6484, USA
            </div>


            <a class="weblink" href="https://website.com/" target="_blank">https://website.com/</a>
            <div class="cmpsocials">
                <ul>
                    <li><a href="https://www.facebook.com/website" target="_blank"><img src="https://website.com/wp-content/themes/jobify/images/facebook-icon.png" alt="facebook"></a></li>
                    <li><a href="https://twitter.com/website" target="_blank"><img src="https://website.com/wp-content/themes/jobify/images/twitter-icon.png" alt="facebook"></a></li>
                    <li><a href="https://www.linkedin.com/website" target="_blank"><img src="https://website.com/wp-content/themes/jobify/images/linkedin-icon.png" alt="facebook"></a></li>
                    <li><a href="https://www.youtube.com/website" target="_blank"><img src="https://website.com/wp-content/themes/jobify/images/youtube-icon.png" alt="facebook"></a></li>
                </ul>

            </div>

        </div>

        <div class="two-btn-listing">
            <!--<div class="cmpny-contact"><a href="<? //get_author_posts_url($user->ID, $author_nicename);  ?>">View Listing</a></div>-->
            <div class="sendmsg elisting-btn" auth-id="JGlassman@website.com"><a href="javascript:void(0)">Send a Message</a></div>

            <div class="search-sp">
                <h4>Energy Supply And Distribution</h4>
                <span>energy suppliers</span> </div>
        </div>

    </div>

</div>

jQuery(".grid-item").click(function(e){

    if(child[i].nodeName != "A"){
        window.location=jQuery(this).find("a.userlink").attr("href"); 
        return false;
    }

});

2 个答案:

答案 0 :(得分:0)

使用jQuery,您可以使用.is()

jQuery(".grid-item").click(function(e) {
  if (!jQuery(e.target).is("a")) {
    location.href = jQuery(this).find("a.userlink").attr("href");
  }
});

您不需要在此使用return false,因为它会停止传播。此外,您可以使用e.target缩短事件的发现。这应该会给你一个很好的开始。

答案 1 :(得分:0)

jQuery(".grid-item a.userlink").click(function(e){
   const href = $(this).attr("href");
   // do whatever you need with the href attr value
});