这是html代码:
<li>
<h3 class="title">
<a href="http://somewebsite.com/pilots/stanley-robert-austin">Stanley Robert Austin</a>
</h3>
<div class="search-snippet-info">
<p class="search-snippet">... 19/03/1897 First Name: Stanley <strong>Robert</strong> Date of Death: Unknown Surname: Austin ... Place of Birth: Foxton, Manawatu Stanley <strong>Robert</strong> Austin Royal Aero Club Cert. No.: 5359 ...</p>
<p class="search-info">The_107s - <a href="/users/admin" title="View user profile." class="username">admin</a> - 15/10/2014 - 11:14</p>
</div>
</li>
<li>
<h3 class="title">
<a href="http://somewebsite.com/pilots/philip-robert-going">Philip Robert Going</a>
</h3>
<div class="search-snippet-info">
<p class="search-snippet">... of Birth: Unknown First Name: Philip <strong>Robert</strong> Date of Death: Unknown Surname: Going Place of Birth: Unknown Philip <strong>Robert</strong> Going Royal Aero Club Cert. No.: 3865 ...</p>
<p class="search-info">The_107s - <a href="/users/admin" title="View user profile." class="username">admin</a> - 15/10/2014 - 11:14</p>
</div>
</li>
我要做的是获取每个列表项不同的标题链接,并将其重复为每个列表元素底部的按钮。这是我到目前为止所提出的:
// Add button to search results. Copy from title
// Search page code.
if ($('body').hasClass('page-search-node')) {
$('ol.search-results li h3 a')
.each(function() {
var n = jQuery(this).find("ol.search-results li h3 a").html();
$(n).clone().insertAfter('.search-snippet-info');
});
}
无法让它发挥作用。感谢任何帮助,谢谢!
答案 0 :(得分:0)
var n = jQuery(this).find("ol.search-results li h3 a").html();
$(n).clone().insertAfter('.search-snippet-info');
应该是:
var n = jQuery(this).html();
$(n).insertAfter('.search-snippet-info');
您不需要再次遍历DOM,它已经遍历。而且,.html()
(您也可以使用.text()
)是一个字符串,不需要克隆。
答案 1 :(得分:0)
您选择了错误的元素,您的代码应该是:
if ($('body').hasClass('page-search-node')) {
$('ol.search-results li')
.each(function() {
var n = jQuery(this).find("h3").find("a").html();
$(n).clone().insertAfter(jQuery(this).find('.search-snippet-info'));
});
}
答案 2 :(得分:0)
我通过将代码更改为:
来解决这个问题// Add button to search results. Copy from title
// Search page code.
if ($('body').hasClass('page-search-node')) {
$('ol.search-results li h3')
.each(function() {
var link = $(this).html();
$(this).parent().find('.search-snippet-info').append(link);
});
}