I have a PHP search script that parses results into HTML div elements to make styling the results easier. I have the following code:
<div class='webresult'><div class='title'><a href='{$row['url']}'>{$row['title']}</a></div><div class='url'>{$row['url']}&l开发者_运维问答t;/div><div class='desc'>{$row['description']}</div></div>
I want to make it so when a the whole webresult div is clicked on, it goes to the url of the result.
I hope people can understand what I'm trying to describe.
If you want to make the entire div clickable, try to style the <a>
element as a block element and make its size equivalent to the parent <div>
, i.e.,
.title a {
display: block;
width: 100%;
height: 100%;
}
use jQuery:
$('.webresult').click(function() {
window.location.href = $(this).find('a').attr('href');
});
isn't
<div onclick="location.href='/the_url/'">
what you need?
Wrap the anchor around everything instead of just the row title, and set it to display: block;
in css. Further style the anchor the way you previously styled the div, and drop the div altogether. You probably also want to set the text color (including for :hover) and remove the underline using the text-decoration property.
If you decide to go with one of the javascript options the others posted, make sure you have a graceful fallback. Not everyone has javascript enabled.
Best to do this via javascript. If using jQuery, you could do this:
$('.webresult').click(function(){
$('this').find('a').click();
})
I would do this with jQuery:
$(".webresult").click(function() {
window.location.href = $(this).find("a").attr("href");
});
Another solution is that within the HTML 5 spec, block elements can actually be links to other places. You can read more about that here: http://html5doctor.com/block-level-links-in-html-5/
精彩评论