jquery从div获取url并将其插入span

时间:2011-03-07 11:04:27

标签: jquery

我想从<div class="ms-vb itx">获取网址,并将其替换为以下范围内的href。 这在Jquery中是可行的吗? 这有意义吗?

<td height="100%" class="ms-vb-title ms-vb-lastCell" onmouseover="OnChildItem(this)">
<div eventtype="" perm="0x7fffffffffffffff" field="LinkTitle" id="5" ctxname="ctx6" onmouseover="OnItem(this)" class="ms-vb itx">
<a target="_self" onclick="EditLink2(this,6);return false;" href="/_layouts/RF.Portal.Web/DetailActualite.aspx?4&amp;ListId={5B770E71-5269-4F45-87A0-2DEFA1136E40}&amp;ID=5&amp;ContentTypeID=0x0100FBEA8B5F8E1448F4B48C33A18522E2DA01001677EA3C9B9802408D15440E3B1906DE" onfocus="OnLink(this)">icitait l'honneur d'être reçu,</a>
<img class="ms-newgif" title="Nouveau" alt="Nouveau" src="/_layouts/1036/images/new.gif">
</div>
<div onmouseover="OnChildItem(this.parentNode); return false;" class="s4-ctx" style="top: 52px; left: 1015px; height: 105px; line-height: 105px; margin: 0px;">
<span>&nbsp;</span>
<a title="Menu Ouvrir" href="javascript:;" onclick="PopMenuFromChevron(event); return false;" onfocus="OnChildItem(this.parentNode.parentNode); return false;">
<img style="visibility: hidden;" src="/_layouts/images/ecbarw.png" alt="Menu Ouvrir"></a>
<span>&nbsp;
</span>
</div>
<span style="color: rgb(128, 128, 128);"><em>
          Publié le 07/03/2011 11:30 -
          En cours</em></span><br> et ayant nom Athos, Porthos et Aramis.   Nous l'avouons, ces trois noms étrange<br><br>
          <span style="font-size: 0.9em; color: rgb(128, 128, 128);" id="lireArticle">
          <a class="rfr-link-action" href="DispForm.aspx?ID=5">Lire l'article</a>
        <br><br></span>
        </td>

2 个答案:

答案 0 :(得分:3)

当然,你必须确保选择正确的元素。

E.g:

var $div = $('div.ms-vb.itx');
$div.next('span').find('a').attr('href', $div.find('a').attr('href'));

如果您有更多元素与这些类,并且您想为每个类执行此操作,您可以使用each()

$('div.ms-vb.itx').each(function() {
    $(this).next('span').find('a').attr('href', $(this).find('a').attr('href'));
});

<强>更新

好的,我假设你的桌子中有几个div,你想为所有这些做这个。

您要更改的a元素似乎有一个类rfr-link-action,因此您可以使用它。请注意,虽然您的span元素具有ID,但ID必须在文档中是唯一的。没有两个元素可以具有相同的ID。

$('div.ms-vb.itx').each(function() {
    $(this).closest('td') // gets a reference to the table cell
    .find('a.rfr-link-action') // finds the link
    .attr('href', $(this).find('a').attr('href')); // sets the URL
});

答案 1 :(得分:0)

试试这个:

url = $('.ms-vb a').attr('href');
$('#your_span_id a').attr('href', url);

但是你应该有办法识别你的divspan,例如添加id属性或smth。