我正在尝试使用 <ul>
创建新闻列表摘要部分并想做整个<li>
可点击。我想使用它在 <li>
中找到的第一个链接作为 URL,以便在未启用 JavaScript 时仍可访问...我的 HTML 是:-
<h3>Regional news</h3>
<ul>
<li class="alt clickable">
<h4><a href="/dave.htm">Fusce porta varius eros</a></h4>
<h5>22 Feb 2009</h5>
<p>Donec bibendum, mauris at vulputate vestibulum, nulla odio eleifend sem, in adipiscing orci neque sit amet ipsum.</p>
</li>
<li class="clickable">
<h4><a href="/dave.htm">Praesent turpis tellus, sagittis eu, molestie ac, posuere id, quam</a></h4>
<h5>18 Feb 2009</h5>
<p>Aliquam mollis. Aliquam erat volutpat. Nulla ultricies ullamcorper magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque hendrerit rhoncus dui.</p>
</li>
<li class="alt clickable">
<h4><a href="/dave.htm">Vivamus libero est, pulvinar vitae, dignissim ut, mollis non, tellus</a></h4>
<h5>24 Feb 2009</h5>
<p>Morbi quis felis. Nunc hendrerit nibh porttitor leo. Aenean ut ipsum sit amet est feugiat bibendum. Vivamus adipiscing purus sed ipsum.</p>
</li>
</ul>
<p class="moreregionalnews"><a href="#">View more regional news</a></p>
所以基本上当用户将鼠标悬停在整个面板上时,整个面板都会发生翻转<li>
不仅仅是<h4>
标签...但使用 <h4>
中包含的 URL标记为链接...
我希望我能明智地解释这一点......我想我在那里混淆了自己:)
干杯
亚当
最佳答案
// when the page is loaded..
$(function() {
// make the cursor over <li> element to be a pointer instead of default
$('li.clickable').css('cursor', 'pointer')
// iterate through all <li> elements with CSS class = "clickable"
// and bind onclick event to each of them
.click(function() {
// when user clicks this <li> element, redirect it to the page
// to where the fist child <a> element points
window.location = $('a', this).attr('href');
return false;
});
});
顺便说一句,您可以仅使用 HTML/CSS(无需 JavaScript)实现相同的功能。但这是另一个问题了。
关于jquery - 如何使用 JQuery 选择整个 <li> 标记并将其设为链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/696028/