通过javascript删除不需要的文本

时间:2015-07-30 09:48:43

标签: javascript jquery html css ajax

我有一张表无法访问HTML代码。我想删除&#39;信息&#39;和&#39; |&#39;来之前#34;注册&#34;链接。我只想要<td>中的注册链接。这就是我想要做的。

<script>
    j$(document).ready(function() {
        j$('td.table-text:contains("|")').replaceWith(" ");
    });
</script>
</head>
<table width="100%" cellspacing="0" cellpadding="0" id="Registrants" class="grid">
    <tbody>
        <tr class="">
            <td class="table-text">
                <input type="checkbox" onclick="AutoCheckGuest('ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_chkRegistrant', 'Registrants', 'ctl00_ContentPlaceHolder1_ctl01_trAddAnother', 'True');" name="ctl00$ContentPlaceHolder1$ctl01$rptRegistrants$ctl00$chkRegistrant" id="ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_chkRegistrant" class="">
                <input type="hidden" value="75a6c1cd-39bc-4cfd-b220-5a8b2bba1ff9" id="ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_hdnRegistrant" name="ctl00$ContentPlaceHolder1$ctl01$rptRegistrants$ctl00$hdnRegistrant">
                <input type="hidden" value="3" id="ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_hdnEntityType" name="ctl00$ContentPlaceHolder1$ctl01$rptRegistrants$ctl00$hdnEntityType">
            </td>
            <td class="table-text">Test Name
                <label class="BodyTextBold1" id="ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_lblPrimaryInvitee" for="ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_chkRegistrant">(Primary Registrant)</label>
            </td>
            <td class="table-text" id="ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_tdRegistrationType">Group Leader&nbsp;</td>
            <td class="table-text"><a href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$ctl01$rptRegistrants$ctl00$btnModifyPersonalInfo&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))" id="ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_btnModifyPersonalInfo" onclick="javascript:return ValidateUnregisterCheckBox('ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_chkRegistrant','You cannot modify information for a guest that is about to be unregistered.');">Information</a> | <a href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$ctl01$rptRegistrants$ctl00$btnModifyRegInfo&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))" id="ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_btnModifyRegInfo" onclick="javascript:return ValidateUnregisterCheckBox('ctl00_ContentPlaceHolder1_ctl01_rptRegistrants_ctl00_chkRegistrant','You cannot modify information for a guest that is about to be unregistered.');">Registration</a>
            </td>
        </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:2)

您可以遍历以查找上一个a中的最后一个td元素并替换其内容。试试这个:

var $td = $('td').last();
var $a = $td.find('a').last();
$td.empty().append($a);

Example fiddle

如果您需要在表的多行上实现此目的,您可以使用:last-child选择器:

var $tds = $('tr td:last-child');
var $a = $tds.find('a').last();
$td.empty().append($a);

Example fiddle