首页 > 解决方案 > 如何使用 JQuery 在标签内查找文本

问题描述

嗨,我有一段如下所示

<p>The strength of the notion of the cultural biography, in my mind, is that it provides us with a way to escape from these preoccupations <xref>1990</xref>.  The algorithm takes a set of earthly 1989 biographies as input and produces a set of improved resurrection 1915 biographies as output.</p>

我需要在标签中找到未标记的年份<p>。我尝试了一个代码,请检查下面

if($xml.find("p").length > 0)
{
    var $element = $xml.find("p").addBack("p");
    $element.each(function()
    {
        if($(this).clone().find('xref').remove().end().text().match(/19+[0-9][0-9]/))
        {
            //*****
        }                           

    });
}

但是这段代码返回单个未标记的年份,我想要一段完整的年份列表

标签: jquery

解决方案


$(document).ready(function(){

var $container=$(this).find("xref").remove().text().match(/\d+/g);


   var num =$(this).find("#value").text().match(/\d+/g).join(",");
    console.log( num);  
         
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<p id="value">The strength of the notion of the cultural biography, in my mind, is that it provides us with a way to escape from these preoccupations <xref>1990</xref> The algorithm takes a set of earthly 1989 biographies as input and produces a set of improved resurrection 1915  biographies as output.</p>


推荐阅读