获取上一个同级的 href 并更新当前项的 href
我最终手工完成了这个工作,但我认为使用一些 jquery 可以更快地完成这个工作。
我有一个表,其中包含下面的数十个示例行。我想做的是找到 的前一个兄弟,获取它们的 URL,将文件类型更改为 .doc 并应用。
<tr id="node-20" class="child-of-node-19">
<td>Tips</td>
<td>
<a href="docs/Interview-Info.pdf">
<img width="20" height="20" src="/images/icons/icon-pdf.gif" alt="PDF" />
</a>
<a href="#">
<img width="20" height="20" src="/images/icons/icon-word.gif" alt="Microsoft Word" />
</a>
</td>
</tr>
因此,一旦 javascript 完成,生成的源代码将如下所示。
<tr id="node-20" class="child-of-node-19">
<td>Tips</td>
<td>
<a href="docs/Interview-Info.pdf">
<img width="20" height="20" src="/images/icons/icon-pdf.gif" alt="PDF" />
</a>
<a href="docs/Interview-Info.doc">
<img width="20" height="20" src="/images/icons/icon-word.gif" alt="Microsoft Word" />
</a>
</td>
</tr>
我的最后一个变体是:
$('a+a[href="#"]').attr('href',$('a+a[href="#"]').prev().attr('href'));
最终获得第一个 url,并将其应用于所有 。因此 Interview-Info.pdf 适用于所有这些链接。
由于时间有限,这是我所能得到的。有一个简单的方法可以做到这一点吗?
I've ended up and done this by hand, but I was thinking this could be done much quicker with some jquery.
I have a table with dozens of the example rows below. What I'd like to do is find the previous siblings of the <a href="#">
, grab their URL, change the filetype to .doc and apply.
<tr id="node-20" class="child-of-node-19">
<td>Tips</td>
<td>
<a href="docs/Interview-Info.pdf">
<img width="20" height="20" src="/images/icons/icon-pdf.gif" alt="PDF" />
</a>
<a href="#">
<img width="20" height="20" src="/images/icons/icon-word.gif" alt="Microsoft Word" />
</a>
</td>
</tr>
So once the javascript is complete, the generated source would look like this.
<tr id="node-20" class="child-of-node-19">
<td>Tips</td>
<td>
<a href="docs/Interview-Info.pdf">
<img width="20" height="20" src="/images/icons/icon-pdf.gif" alt="PDF" />
</a>
<a href="docs/Interview-Info.doc">
<img width="20" height="20" src="/images/icons/icon-word.gif" alt="Microsoft Word" />
</a>
</td>
</tr>
My last variation was:
$('a+a[href="#"]').attr('href',$('a+a[href="#"]').prev().attr('href'));
That ends up getting the first url, and applying it to all the <a href="#">
. So Interview-Info.pdf is applied to all those links.
This is as far as I got due to time contraints. Is there a simple was to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.attr() 仅适用于集合中的第一个元素,因此您可能需要迭代您的列出并将其应用于每个:
.attr() only works with the first element in the set, so you probably want to iterate over your list and apply it to each:
你就快到了,试试这个:
you're almost there, try this: