jQuery 用于在列表项中设置唯一 href 属性的代码
jQuery code to set unique href attributes within list items
我有一个包含 200 个项目的清单。每个 <li>
包含一个 <h3>
标签,其中的文本字符串具有唯一的 phone 数字。每个 <li>
还包含 <a>
标签,<h3>
的父标签。在页面加载时,我需要使用其子 <h3>
中包含的 phone 数字设置每个 <a>
标签的 href 值。
jQuery
$("a").each(function() {
var phonenumber = $(this > "h3").html();
$(this).attr("href", "phonenumber");
});
HTML
<li>
<h1>Altman, Jennifer, Phd</h1>
<a href="">
<h3>617-999-5054</h3>
</a>
</li>
我有一个包含 200 个项目的清单。每个 <li>
包含一个 <h3>
标签,其中的文本字符串具有唯一的 phone 数字。每个 <li>
还包含 <a>
标签,<h3>
的父标签。在页面加载时,我需要使用其子 <h3>
中包含的 phone 数字设置每个 <a>
标签的 href 值。
jQuery
$("a").each(function() {
var phonenumber = $(this > "h3").html();
$(this).attr("href", "phonenumber");
});
HTML
<li>
<h1>Altman, Jennifer, Phd</h1>
<a href="">
<h3>617-999-5054</h3>
</a>
</li>