为什么jQuery .text()方法不能从td中获取文本

时间:2014-10-14 17:55:38

标签: jquery html

我正在尝试从许多不同的电子邮件中获取电子邮件地址,并且我正在尝试使用jQuery选择器来获取这些电子邮件地址。但是,我不断收到消息TypeError: undefined is not a function

html如下所示:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Activate Your Groupon Account</title>
      <link rel="important stylesheet" href="">
      <style>div.headerdisplayname {font-weight:bold;}</style>
   </head>
   <body>
      <table border=0 cellspacing=0 cellpadding=0 width="100%" class="header-part1">
         <tbody></tbody>
         <tr>
            <td>
               <div class="headerdisplayname" style="display:inline;">Subject: </div>
               Activate Your Groupon Account
            </td>
         </tr>
         <tr>
            <td>
               <div class="headerdisplayname" style="display:inline;">From: </div>
               Groupon <notify@r.groupon.com>
            </td>
         </tr>
         <tr>
            <td>
               <div class="headerdisplayname" style="display:inline;">Date: </div>
               10/8/2014 12:25 PM
            </td>
         </tr>
      </table>
      <table border=0 cellspacing=0 cellpadding=0 width="100%" class="header-part2">
         <tr>
            <td>
               <div class="headerdisplayname" style="display:inline;">To: </div>
               myEmail@gmail.com
            </td>
         </tr>
      </table>
      <br>
      <div class="moz-text-html"  lang="x-unicode">
         <html>
            <head>
               <title>Groupon | Wednesday, October 8, 2014</title>
               <meta http-equiv="Content-Type" content="text/html; ">
            </head>
            <body style="height:100%; margin:0!important; padding:0!important;-webkit-text-size-adjust:none; background:#89B556;">
               <img src = "http://www.groupon.com/analytic/track.gif?p=1&utm_domain=gmail.com&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da&utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008" height="1" width="1" />    
               <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; width:100%; background:#89B556;" bgcolor="#89B556" valign="top" align="center">
                  <tr>
                     <td style="width:33%;" valign="top">&nbsp;</td>
                     <td style="width:33%; padding: 0 25px 40px 25px;" valign="top" align="center">
                        <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333;" width="600" align="center">
                           <tr>
                              <td style="padding: 0 0 0 10px;" align="left" valign="bottom">
                                 <a href="http://www.groupon.com/?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=header&c=logo&d=index&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da"><img src="http://s3.grouponcdn.com/email/images/global_images/logo_pg_top.png" style="display: block; border: none; color:#fff; font-size:22px; font-weight:bold;" alt="GROUPON" title="GROUPON"></a>
                              </td>
                           </tr>
                           <tr>
                              <td style="-moz-border-radius: 4px 4px 0 0; -webkit-border-radius: 4px 4px 0 0; border-radius: 4px 4px 0 0; -khtml-border-radius: 4px 4px 0 0; background:#fff; padding: 0 0 0 10px;" align="left" valign="top"><a href="http://www.groupon.com/?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=header&c=logo&d=index&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da"><img src="http://s3.grouponcdn.com/email/images/global_images/logo_pg_bottom.gif" style="display: block; border: none; color:#fff; font-size:22px; font-weight:bold;" alt="" title=""></a></td>
                           </tr>
                           <tr>
                              <td style="background:#fff;">
                                 <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; width:100%;" align="center">
                                    <tr>
                                       <td>
                                          <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333;">
                                             <tr>
                                                <td style="padding:0 18px 20px 25px;">
                                                   <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; width:540px;text-align: left; line-height:18px; font-weight: 100;" width="540">
                                                      <tr>
                                                         <td style="font-size: 22px; color:#82b548; padding: 20px 0 30px 0; font-weight: bold; line-height:28px;">Thanks for Joining!</td>
                                                      </tr>
                                                      <tr>
                                                         <td style="font-family:arial; font-size:20px; color:#949494; line-height: 26px;">You’re going to love it</td>
                                                      </tr>
                                                      <tr>
                                                         <td style="border-bottom: 1px solid #cfe1bc; font-size: 14px; padding: 20px 40px 20px 0;">Check your inbox every day to discover Groupon deals with huge discounts on tasty meals, relaxing spa days, concerts, 5-star hotels and more.</td>
                                                      </tr>
                                                      <tr>
                                                         <td style="padding: 30px 0 10px 0;">To complete your registration for Groupon, please verify your email.</td>
                                                      </tr>
                                                      <tr>
                                                         <td style="padding: 10px 0 20px 0;">
                                                            <table cellpadding="0" cellspacing="0" style='font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333;'>
                                                               <tr>
                                                                  <td height="40" width="185" bgcolor="#f98b62" background="http://s3.grouponcdn.com/email/images/global_buttons/gradient_orange_sm.gif" background-repeat="repeat-x" style="height: 40px; -moz-border-radius: 8px 8px 8px 8px; -webkit-border-radius: 8px 8px 8px 8px; border-radius: 8px 8px 8px 8px; -khtml-border-radius: 8px 8px 8px 8px; border: 1px solid #f55d16; text-align: center;">
                                                                     <a href="https://www.groupon.com/users/activate/8eceddecb9f30c3384fb2507b0d234805698010c?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=secondary-content-area&c=button&d=verify-account&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da" style="padding:5px 20px 5px 20px; color: #fff; text-shadow:  1px 1px 1px #b2552d; font-size: 18px; text-decoration: none; font-weight: bold; display: block;" target="_blank" title="Verify Account">Verify Account</a>
                                                                  </td>
                                                               </tr>
                                                            </table>
                                                         </td>
                                                      </tr>
                                                   </table>
                                                </td>
                                             </tr>
                                          </table>
                                          <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; border-top: 1px solid #cccccc; text-align:left;">
                                             <tr>
                                                <td style="padding: 15px 40px 15px 40px;">
                                                   <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333;">
                                                      <tr>
                                                         <td><a href="https://www.groupon.com/myprofile?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=secondary-content-area&c=image&d=personalize&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da" style="color:#0185C6; text-decoration:none;"><img src="http://s3.grouponcdn.com/email/images/welcome-email/personalize.gif" style="display: block; border: none;" alt="Personalize your deals" title="Personalize your deals"></a></td>
                                                         <td style="padding: 0 0 0 20px;">
                                                            <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; font-weight: 100; text-align: left;">
                                                               <tr>
                                                                  <td><a href="https://www.groupon.com/myprofile?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=secondary-content-area&c=text-link&d=personalize&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da" style="color:#0185C6; text-decoration:none; font-weight: bold;">Personalize your deals</a></td>
                                                               </tr>
                                                               <tr>
                                                                  <td style="padding: 5px 0 0 0;">Help us get you the best deals by completing your profile.</td>
                                                               </tr>
                                                            </table>
                                                         </td>
                                                      </tr>
                                                   </table>
                                                </td>
                                             </tr>
                                          </table>
                                          <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; border-top: 1px solid #cccccc; text-align: left;">
                                             <tr>
                                                <td style="padding: 15px 40px 15px 40px;">
                                                   <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; text-align:left;">
                                                      <tr>
                                                         <td><a href="http://www.groupon.com/groupon-promise?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=secondary-content-area&c=image&d=groupon-promise&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da" style="color:#0185C6; text-decoration:none;"><img src="http://s3.grouponcdn.com/email/images/welcome-email/promise.gif" style="display: block; border: none;" alt="We got your back" title="We got your back"></a></td>
                                                         <td>
                                                            <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; font-weight: bold; color: #5f8237;">
                                                               <tr>
                                                                  <td>The</td>
                                                               </tr>
                                                               <tr>
                                                                  <td>GROUPON</td>
                                                               </tr>
                                                               <tr>
                                                                  <td style="padding: 0 30px 25px 0;">Promise</td>
                                                               </tr>
                                                            </table>
                                                         </td>
                                                         <td style="padding: 0 0 0 20px;">
                                                            <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; font-weight:100; text-align:left;">
                                                               <tr>
                                                                  <td><a href="http://www.groupon.com/groupon-promise?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=secondary-content-area&c=text-link&d=groupon-promise&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da" style="color:#0185C6; text-decoration:none; font-weight: bold;">We got your back</a></td>
                                                               </tr>
                                                               <tr>
                                                                  <td style="padding: 5px 0 5px 0;">If the experience using your Groupon ever lets you down, we’ll make it right or return your purchase.</td>
                                                               </tr>
                                                               <tr>
                                                                  <td>Simple as that.</td>
                                                               </tr>
                                                            </table>
                                                         </td>
                                                      </tr>
                                                   </table>
                                                </td>
                                             </tr>
                                          </table>
                                       </td>
                                    </tr>
                                 </table>
                              </td>
                           </tr>
                        </table>
                        <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; width:600px; border-top: 1px solid #7a9e32; text-align: center; " width="600" background="#ddeacf" bgcolor="#ddeacf" align="center">
                           <tr>
                              <td style="padding: 10px 30px 10px 30px; color:#666; font-family: Arial, Helvetica, sans-serif; font-weight: bold;" align="center">
                                 Need help? <span style="color:#0185C6; text-decoration:none;"><a href="https://www.groupon.com/helpcenter?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=contact-block&c=text-link&d=contact-groupon&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da" style="color:#0185C6; text-decoration:none;">Contact Groupon</a></span>
                              </td>
                           </tr>
                        </table>
                        <table cellpadding="0" cellspacing="0" style="font-family: Arial, Helvetica, sans-serif; font-size:14px; line-height:19px; color:#333; background:#eff5e7; width:600px; -moz-border-radius: 0 0 4px 4px;  border-radius: 0 0 4px 4px; -webkit-border-radius: 0 0 4px 4px; -khtml-border-radius: 0 0 4px 4px;" align="center" width="600">
                           <tr>
                              <td style="padding: 10px 0 10px 0; font-size: 10px; text-align: center;">Delivered by Groupon Inc. 600 W. Chicago Avenue, Suite 400 Chicago, IL, 60654, USA</td>
                           </tr>
                           <tr>
                              <td style="padding: 0 20px 0 20px; font-size:10px; line-height: 14px; color:#666666; text-align: left;" valign="top">
                                 You are receiving this email because you signed up for the Daily Groupon alerts. If you prefer not to receive the daily Groupon email, you can always <a href="https://www.groupon.com/subscription_center/fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106?mobile=false&utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=footer&c=link&d=subs-center&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da" style="color:#0185C6; text-decoration:none;" target="_blank">manage your subscriptions</a>. Be sure to add us to your address book or safe sender list so our emails get to your inbox. <a href="http://www.groupon.com/pages/whitelist?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=footer&c=link&d=groupon_whitelist&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da" style="color:#0185C6; text-decoration:none;" target="_blank">Learn how</a>
                              </td>
                           </tr>
                           <tr>
                              <td align="center" style="padding: 20px 0 20px 0;"><a href="http://www.groupon.com?utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008&s=footer&c=logo&d=index&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da"><img src="http://s3.grouponcdn.com/email/images/global_images/footer_pg_ico.gif" style="display: block; border: none;" alt="GROUPON" title="GROUPON"></a></td>
                           </tr>
                        </table>
                     </td>
                     <td style="width:33%;" valign="top">&nbsp;</td>
                  </tr>
               </table>
               <img src = "http://www.groupon.com/analytic/track.gif?p=2&utm_domain=gmail.com&utm_campaign=55dd4215-7b6d-4818-9a72-0da61a4540da&utm_source=activation_notification&utm_medium=email&sid=55dd4215-7b6d-4818-9a72-0da61a4540da&user=fb4de52d8d1af92fd57e6e895aa7c9a0f846d88bd27c203571f4e3776baa6106&date=20141008" height="1" width="1" />  
            </body>
         </html>
      </div>
   </body>
</html>
</table></div>

我用来选择它的代码有效:

$('tr:nth-child(2) td')

但是一旦我尝试抓住文本它就会破坏:

$('tr:nth-child(2) td').text()

4 个答案:

答案 0 :(得分:0)

由于新要求而修订的答案:

检查这个小提琴:http://jsfiddle.net/2uvk9zxp/ 假设您想要的电子邮件每次都在完全相同的位置,您将不得不抓住某些元素并将其拆分到某些地方。

这是我正在使用的javascript:

var getEmail = $('tr:nth-of-type(2) td').html().split("Groupon ")[1].split(" ")[0].replace("<","").replace(">","");
console.log(getEmail); 

END REVISED

问题是您的<tbody>未包含在<table>内。正如Magnus Engdal所说,它是无效的标记,因此javascript无法正确读取。

您所要做的就是将表格包装在实际的<table></table标签中。

这里有一个示例WITHOUT表标签的小提琴: http://jsfiddle.net/h58hvxxk/2/

以下是WITH表标签的示例: http://jsfiddle.net/h58hvxxk/3/

您可以看到提示框工作正常,拉出您要查找的文字。 - 这个功劳归功于马格努斯,所以如果他写下同样的答案,你需要接受并投票给他。 - P.S.不要让我写一个(1)而不是(2)的事实阻止你看到它运作正常。

答案 1 :(得分:0)

我与所有未被TS读取的评论联系在一起。

  1. 您的表格结构没有问题。

  2. 错误TypeError: undefined is not a function告诉我们 你的jQuery没有加载,js什么都不知道 约$()结构或您在页面上有冲突 另一个js框架(如mootools)。为了解决这个问题 冲突 - 使用jQuery('tr:nth-child(2) td').text()

  3. 使用js代码显示页面的完整代码,而不仅仅是其中的一部分。

  4. <notify@r.groupon.com>无法通过.text()函数访问,因为浏览器认为这是一个标记。使用.html()并解析。看这里 - http://jsfiddle.net/u6oa25b5/1/

  5. 可以在不解析html代码的情况下这样做。

  6. 喜欢这个(此处的电子邮件位于div标记内,仅为了简单起见) - http://jsfiddle.net/u6oa25b5/2/

    var email = $('div').find('*').filter(function(){
      if (this.tagName.search(/@/) !== -1)
          return this;
    });
    
    alert(email[0].tagName);
    

答案 2 :(得分:0)

尝试

HTML

      <td>
         <div class="headerdisplayname" style="display:inline;">From: </div>
          <!-- wrap email address inside `span` tag -->
          Groupon <span>notify@r.groupon.com</span>
      </td>

JS

// apply rendering of `<` , `>` around email address text in `span`
var email = $("tr:nth-child(2) td span").before("<").after(">").text();
console.log(email)

<替换文字>html。如果要将电子邮件地址呈现为实际可导航的链接? ,换行包裹在a(锚)标记中,然后将a标记的href属性设置为mailto:notify@r.groupon.com

&#13;
&#13;
var email = $("tr:nth-child(2) td span").before("<").after(">").css("color", "blue").text();
console.log(email)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table border=0 cellspacing=0 cellpadding=0 width="100%" class="header-part1">
   <tr>
      <td>
         <div class="headerdisplayname" style="display:inline;">Subject: </div>
         Activate Your Groupon Account
      </td>
   </tr>
   <tr>
      <td>
         <div class="headerdisplayname" style="display:inline;">From: </div>
          Groupon <span>notify@r.groupon.com</span>
      </td>
   </tr>
   <tr>
      <td>
         <div class="headerdisplayname" style="display:inline;">Date: </div>
         10/8/2014 12:25 PM
      </td>
   </tr>
</table>
<table border=0 cellspacing=0 cellpadding=0 width="100%" class="header-part2">
   <tr>
      <td>
         <div class="headerdisplayname" style="display:inline;">To: </div>
         myEmail@gmail.com
      </td>
   </tr>
</table>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

其他所有答案都没有用,这可能是由于此页面上有jQuery的内容。 $选择器有效,但输入jQuery未定义。

这最终有效:

document.querySelectorAll('.headerdisplayname')[1].parentNode.children[1].tagName

document.querySelectorAll('tr > td')[1].children[1].tagName