简体   繁体   English

在haml中将div或div作为href

[英]div into link or div as href in haml

Given this 鉴于这种

<div class='postpreview'>
  <a href="/some/image/xyz"><img alt="xyz" src="/some/image/xyz" /></a>
  Some content, etc.
  <a href='/forums/post/123'>Show</a>
</div>

How can I make the entire div 'postpreview' function as link to '/forums/post/123'? 如何使整个div的“ postpreview”功能链接为“ / forums / post / 123”? In haml? 在哈姆尔?

Basically this is within a rails partial, and the links are listed as link_to, etc (another issue altogether). 基本上,这是在rails部分内,并且链接被列为link_to等(共另一个问题)。 I can get the above to work fine in haml, I just cannot turn the entire div into a link without either an error or the output showing up on the page but not linking. 我可以使以上内容在haml中正常运行,我只是无法将整个div转换为链接而不会出现错误,也不会在页面上显示输出但没有链接。 I'd like to get rid of the 'Show' link and make the entire postpreview link to the post. 我想摆脱“显示”链接,并把整个postpreview链接设为该帖子。

You'd need to use JavaScript to add a click hanlder to the div element. 您需要使用JavaScript将点击处理程序添加到div元素。 For example, if you give the particular div an id it is as simple as: 例如,如果给特定的div一个id,它就很简单:

document.getElementById('myDivID').onclick = function() {
    window.location = '/forums/post/123'; 
}

Or if you're using jQuery: 或者如果你正在使用jQuery:

$('#myDivId').click(function() { window.location = ... });

Note that you've got two links within the div - is it intentional to make the first link impossible to follow? 请注意,div中有两个链接-是否有意使第一个链接无法跟随?

I think Hamish is sure right with his question.. What about the first link? 我认为Hamish对他的问题肯定是正确的。 Do you need it? 你需要它吗? Else you could just kill the first and go with some pure HTML/CSS solution which extends every a in a div with the class postpreview: 否则,您可以杀死第一个,然后使用一些纯HTML / CSS解决方案,该解决方案使用class postpreview在div中扩展每个a:

HTML HTML

<div class="postpreview">
 <a href="/forums/post/123" rel="bookmark" title="Foo">
  <img src="/some/image/xyz.jpg" alt="foo" title="bar" />  
 </a>                        
</div>

CSS CSS

.postpreview a {
  display: block;
  width: 100%;
  height: 100%;
}

respectively set the height and width of the a element according to your image size or better the surrounding div. 根据您的图片大小或更好的周围div分别设置a元素的高度和宽度。

This is a solution -- a link to the image itself would be nice, or rather is the next step - generating this postpreview that links to post with the thumbnail inside linking to the image 这是一个解决方案 - 图像本身的链接会很好,或者更确切地说是下一步 - 生成此postpreview链接到post,其中缩略图链接到图像

The line prefaced by / does not work so well, but the open image_tag line does link to page the post is on, no js required. 以/开头的行不能很好地工作,但open image_tag行确实链接到帖子打开的页面,不需要js。

.postpreview
 %a{:href =>(url_for [@blog, post])}
  .postcontaincontain
   = image_tag(post.image_url(:thumb)) if post.image_url
   /= link_to image_tag(post.image_url(:thumb)), post.image_url if post.image_url
   = shout.content

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 <div>链接打不开 - <a href=link><div></a> link not woking 使用定位点将href链接到下一个div - Link href to the next div with anchorpoint div:href和所有链接 - div with :href and a link over it all 如何添加“a href”链接到“div”? - How To Add An “a href” Link To A “div”? 如何使用href链接删除div? - How to remove a div with an href link? 整个div作为链接href,但不输入文本 - whole div as link href but not input text 单击href链接,并且div显示图像 - a href link clicked and div displays image 如何拥有一个整体<div>包含带有 href 的链接</div><div id="text_translate"><p>我正在尝试将“signup.php”的&lt;a href&gt;链接更改为 go 从NOW的单词到整个&lt;div&gt; ,因此当用户将鼠标悬停在该 div 内的任何内容上时,该链接将起作用:</p><p> <strong>HTML:</strong></p><pre> &lt;div class="row"&gt; &lt;div class="grid-full containerHomepage"&gt; &lt;div class="centered"&gt;SIGN UP TO HEAD SMART&lt;a class="noDecoration brain" href="signup.php"&gt; NOW&lt;/a&gt; &lt;img src="images/HeadSmart.png" width="50" height="60"/&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre></div> - How to have a whole <div> contain a link with a href 显示一个 <div> 当用户单击带有href“#”的链接时 - Show a <div> when the user clicks a link with href “#” Prototype隐藏具有特定链接的div( - Prototype hide the div that has a specific link (<a href)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM