Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么jQuery.text()方法不能从td抓取文本_Jquery_Html - Fatal编程技术网

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

为什么jQuery.text()方法不能从td抓取文本,jquery,html,Jquery,Html,我试图从许多不同的电子邮件中获取电子邮件地址,并尝试使用jQuery选择器来获取这些地址。但是,我一直收到消息TypeError:undefined不是一个函数 html显示如下: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Activate Your Groupon Account

我试图从许多不同的电子邮件中获取电子邮件地址,并尝试使用jQuery选择器来获取这些地址。但是,我一直收到消息TypeError:undefined不是一个函数

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').text()

因新要求而修订的答案:

检查这把小提琴: 假设你想要的电子邮件每次都在同一个地方,你必须抓住某些元素并在某些地方拆分它

以下是我使用的javascript:

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

问题在于,您的代码没有包含在一个脚本中,正如Magnus Engdal所说,它是无效的标记,因此javascript无法正确读取


你所要做的就是把你的表用实际的包装起来,我被TS没有读到的所有评论捆绑住了

您的表结构没有问题

错误类型error:undefined不是一个函数 要么jQuery没有加载,要么js什么都不知道 关于$structure或您在页面上与 另一个js框架,比如mootools。为了解决这个问题, 冲突-使用jQuery'tr:nth-child2 td'。文本

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

无法通过.text函数访问,因为浏览器认为这是一个标记。使用.html和parse。看这里-

不解析html代码就可以这样做

就像这封电子邮件在div标签中一样,为了简单起见-

试一试

html

主题: 激活您的Groupon帐户 发件人: Grouponnotify@r.groupon.com 日期: 2014年8月10日下午12:25 致: myEmail@gmail.com
其他答案都不适用于此,这可能是由于本页上的jQuery出现了问题。$selector起作用,但jQuery中的键入未定义

这就是最终起作用的原因:

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


您也可以显示父元素吗?看起来您的电子邮件信息被解释为html标记。不确定该数据是如何生成的,但表示开始和结束标记的小于和大于符号应编码为,并且您确定jQuery加载正确吗?这是运行Javascript的正常位置。@MagnusEngdal是正确的。关键是:-在jQuery能够很好地处理HTML元素之前,必须使它们有效。问题与表结构无关。即使结构错误,也不会有这样的错误消息。jsfiddle.net/u6oa25b5@Cheery,使用给定的代码,当我添加表标记时,它工作得很好。有了新的代码和刚刚提供的小提琴,它也可以很好地工作。除了没有显示完整的javascript之外,我看不到任何问题。他只是展示了他是如何选择的-但我们不知道他是否允许它发射。有没有一种方法可以让我不使用tbody标签抓取这封电子邮件,因为我不是这种格式的作者?@maudulus你不需要使用tbody标签,不要听他的。你的桌子一切都很好。问题与此无关。只是好奇,TS的意思是什么?Thanks@guest271314TS=topic start但在jQuery中键入是未定义的,这是我告诉您的意思-您没有加载jQuery,但页面上有类似mootools的内容,它还使用$symbol!重复了好几次。如果您不想使用jQuery,那么答案将是纯javascript。如果您从一开始就展示了页面的完整代码,那么就不会有太多的评论和讨论。
var email = $('div').find('*').filter(function(){
  if (this.tagName.search(/@/) !== -1)
      return this;
});

alert(email[0].tagName);
      <td>
         <div class="headerdisplayname" style="display:inline;">From: </div>
          <!-- wrap email address inside `span` tag -->
          Groupon <span>notify@r.groupon.com</span>
      </td>
// apply rendering of `<` , `>` around email address text in `span`
var email = $("tr:nth-child(2) td span").before("<").after(">").text();
console.log(email)
document.querySelectorAll('.headerdisplayname')[1].parentNode.children[1].tagName
document.querySelectorAll('tr > td')[1].children[1].tagName