简体   繁体   English

如何设置整个Div目录像HyperLink一样可点击?

[英]How to set whole Div Contents clickable like a HyperLink?

I am developing website by using ASP.net. 我正在使用ASP.net开发网站。 I have following div layout 我有以下div布局

 <div class="mainrepeater">
    <div id="image" class="my-ad-repeater-image-box"/>
    <div class="my-repeater-title">
          <asp:HyperLink ID="hlNavigation" runat="server" Text='<%# Eval("title") %>'         NavigateUrl='<%#Eval("ad_url") %>' Target="_blank"></asp:HyperLink></div>
    <div class="my-repeater-content"></div>
 </div>

I am setting the HyperLink navigate URL from a datasource and everythings works fine. 我正在从数据源设置HyperLink导航URL,一切正常。 But I want all div(mainrepeater) to be clickable instead of the hyperlink. 但我希望所有div(mainrepeater)都可以单击,而不是超链接。

So how to achieve that?. 那么如何实现呢? Do I need to use javascript? 我需要使用JavaScript吗? If not that would be great. 如果不是这样,那就太好了。

Thank you very much. 非常感谢你。

CSS 的CSS

.my-repeater-title { cursor: pointer; }

JS JS

$(".my-repeater-title").click(function(){
    window.location.href = "http://example.com"
 });

You should use attribute data-* to retrieve your url on your js script as: 您应该使用data- *属性来检索js脚本上的网址,如下所示:

<div class="my-repeater-title" data-url="[url]">

And get on your script: 并获取您的脚本:

$(".my-repeater-title").on('click', function(){
    var target = $(this).data('url');
    window.location.href = target;
 });

It's recommended to not write external data like url directly to the js file, but to fetch on html by js. 建议不要将外部数据(例如url)直接写入js文件,而应通过js提取html。

Another possible option is to wrap <div class="mainrepeater"> with <a> tag. 另一个可能的选择是用<a>标记包装<div class="mainrepeater">

But this is correct only for HTML5. 但这仅适用于HTML5。 For more details please check this post Is putting a div inside an anchor ever correct? 有关更多详细信息,请检查此帖子将div放入锚点是否正确?

HTML 的HTML

<a class="mainrepeater_link" href="http://example.com">
    <div class="mainrepeater"> ... </div>
</a>

CSS (only if you target a HTML version less than 5.) CSS (仅当您定位的HTML版本小于5时)。

.mainrepeater_link { display: block; }

Three possible solutions come to mind 我想到了三种可能的解决方案

First idea: Make the size of the link bigger 第一个想法:增大链接的大小

If the link is the only content of your div, you can just add the following CSS to make it fill the div. 如果链接是div的唯一内容,则只需添加以下CSS以使其填充div。

.my-repeater-title > a 
{
  display:block;
  width:100%;
  height:100%;
}

You might need to set dimensions on .my-repeater-title though. 不过,您可能需要在.my-repeater-title上设置尺寸。 No JS required 不需要JS

Second idea 第二个主意

Swap the div and the link. 交换div和链接。 Change 更改

<div class="my-repeater-title">
  <a href="...">...</a>
</div>

to

<a href="...">
  <div class="my-repeater-title">
    ...
  </div>
</a>

I'm sure that's possible in ASP too. 我确信在ASP中也有可能。 No JS required either 也不需要JS

Third idea: Javascript 第三个主意:JavaScript

Add a click-handler in jquery. 在jquery中添加点击处理程序。 This has already been suggested by others, so I won't copy their solution and I won't bother writing a different one. 其他人已经建议过这一点,所以我不会复制他们的解决方案,也不会费心编写其他解决方案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM